RTK generateEndpoints throws typescript error: Trace: TypeError: Cannot read property 'pos' of undefined
See original GitHub issueRunning the RTK code generation as described in the docs by running npx @rtk-query/codegen-openapi openapi-config.ts
works fine without any issues.
However, running exactly the same config using the generateEndpoints
function produces what looks like a typescript error. I’ve tried running it both using Node and esbuild-runner but it still errors. I’ve tried using different openapi endpoints but I still receive the same error when trying to run the function programmatically.
Setup
// src/store/emptyApi.ts
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
export const emptySplitApi = createApi({
baseQuery: fetchBaseQuery({ baseUrl: '/' }),
endpoints: () => ({}),
});
// apiConfig.ts
import type { ConfigFile } from '@rtk-query/codegen-openapi';
const apiConfig: ConfigFile = {
// Using an example openapi endpoint found online
schemaFile: 'https://petstore.swagger.io/v2/swagger.json',
apiFile: './src/store/emptyApi.ts',
apiImport: 'emptySplitApi',
outputFile: './src/model/generated/api.ts',
exportName: 'api',
hooks: true,
};
export default apiConfig;
// generateApi.ts (run through node or esbuild-runner)
const config = {
schemaFile: 'https://petstore.swagger.io/v2/swagger.json',
apiFile: './src/store/emptyApi.ts',
apiImport: 'emptySplitApi',
outputFile: './src/model/generated/api.ts',
exportName: 'api',
hooks: true,
};
generateEndpoints(config).catch(console.trace);
Output
Trace: TypeError: Cannot read property 'pos' of undefined
at emitNodeList (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:111266:48)
at emitList (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:111158:13)
at emitObjectLiteralExpression (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:109538:13)
at pipelineEmitWithHintWorker (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:108863:32)
at pipelineEmitWithHint (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:108479:17)
at pipelineEmitWithComments (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:112033:13)
at pipelineEmit (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:108419:13)
at emitExpression (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:108403:13)
at emitNodeList (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:111271:25)
at emitExpressionList (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:111161:13)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
Issue Analytics
- State:
- Created a year ago
- Comments:13 (5 by maintainers)
Top Results From Across the Web
"TypeError: Cannot read property 'pos' of undefined" after ...
I'm trying to clear data from the all charts (Highcharts) in my Angular application. After all i am getting an error as in...
Read more >How to Fix TypeError: Cannot read Property 'push' of ...
You call the method on a variable previously set to undefined . You call the method on a variable before it has been...
Read more >How to Avoid the Infamous "Cannot read properties of ... - Bitovi
As a JavaScript developer, I'm sure you've encountered the frustrating runtime TypeError Cannot read properties of undefined . TypeScript ...
Read more >How to Read React Errors (fix 'Cannot read property of ...
Got an error like this in your React component? Cannot read property `map` of undefined. In this post we'll talk about how to...
Read more >Avoiding those dang cannot read property of undefined errors
Uncaught TypeError: Cannot read property 'foo' of undefined. The dreaded error we all hit at some point in JavaScript development.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The function that throws the error seems mostly the same between these versions:
https://raw.githubusercontent.com/microsoft/TypeScript/v4.5.2/lib/typescript.js
https://raw.githubusercontent.com/microsoft/TypeScript/v4.7.4/lib/typescript.js
Can see that
node.elements.end
is the offending object/array. I’ll keep digging and update if I find anything.Edit: Looks to me like
filterEndpoints
andendpointOverrides
are the two possible properties that can beundefined
and also use a possible List/NodeList and Tuple type? The stack trace mentions aUnionType -> List -> NodeList -> Tuple
. I need to fix some stuff in my project and then I can test this theory out, I’ll update as soon as I can.Yeah, in my project
oazapfts
itself is usingtypescript
4.7.4 whilertk-query-codegen-openapi
is using 4.5.5. Sinceoazapfts
is used for typescript generation, nodes are being generated using 4.7.4 and then being processed by this library using 4.5.5.In my case this is due to how I have yarn set up, using
yarn
3.2.1 workspaces withnodeLinker: node-modules
in.yarnrc.yml
.I tried adding a resolution to force everything to use 4.7.4 and everything is working so far:
Obviously this is risky to force libraries to use a version they don’t expect but for now it’s allowing development on this front to continue. 😃