-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
RTK generateEndpoints throws typescript error: Trace: TypeError: Cannot read property 'pos' of undefined #2425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Have you tried calling See redux-toolkit/packages/rtk-query-codegen-openapi/src/bin/cli.ts Lines 56 to 71 in a5cf61b
|
Yes, I've tried it and it gave me what looked like the same error. However, when double checking i saw that it was slightly different (Albeit still seems to be a typescript parsing error, and I still have no idea why it's happening):
Using this code: const config = parseConfig({
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
});
config.forEach((c) => generateEndpoints(c).catch(console.trace)); |
I'm running into the same issue:
Also tried adding parseConfig but still getting the same error. I did have this working in a previous version, running into this after updating Edit: I wouldn't mind digging into this. Is there some way to get a trace that leads me in the right direction? I'm really just not sure where I would start since the trace is all typescript library file. |
I just tried this and for me it is working. Maybe it is your TypeScript version? I just tried with 4.5.2. |
Ah yeah, didn't think of that. Currently, Typescript 4.7.4. I'll test with 4.5.2 and update here. |
I bumped up against this too. Using typescript 4.7.4 throws the error described above, but reverting to typescript v4.5.2 fixes it. |
Could you take a look what changed between those versions and if you could come up with a fix? |
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 function emitTupleType(node) {
emitTokenWithComment(22 /* OpenBracketToken */, node.pos, writePunctuation, node);
var flags = ts.getEmitFlags(node) & 1 /* SingleLine */ ? 528 /* SingleLineTupleTypeElements */ : 657 /* MultiLineTupleTypeElements */;
emitList(node, node.elements, flags | 524288 /* NoSpaceIfEmpty */);
emitTokenWithComment(23 /* CloseBracketToken */, node.elements.end, writePunctuation, node);
} https://raw.githubusercontent.com/microsoft/TypeScript/v4.7.4/lib/typescript.js function emitTupleType(node) {
emitTokenWithComment(22 /* SyntaxKind.OpenBracketToken */, node.pos, writePunctuation, node);
var flags = ts.getEmitFlags(node) & 1 /* EmitFlags.SingleLine */ ? 528 /* ListFormat.SingleLineTupleTypeElements */ : 657 /* ListFormat.MultiLineTupleTypeElements */;
emitList(node, node.elements, flags | 524288 /* ListFormat.NoSpaceIfEmpty */, parenthesizer.parenthesizeElementTypeOfTupleType);
emitTokenWithComment(23 /* SyntaxKind.CloseBracketToken */, node.elements.end, writePunctuation, node);
} Can see that Edit: Looks to me like |
Edit: False positive because I was just filtering every endpoint. I added regex to include everything so that I could pass a value to filterEndpoints without it being undefined but I still get the error. Still looking. |
Reverting to typescript version {
"devDependencies": {
...
"typescript": "=4.5.2"
},
"resolutions": {
"@rtk-query/codegen-openapi/typescript": "=4.5.2"
},
} But still getting the same issue:
Running
|
I am able to reproduce this error in TS version 4.7.4 (but not on 4.3.5). I am running Node.js version 16.13.0 on Windows. I'm hoping that I'll get a chance to dig into it and figure out how we can make the codegen compatible with the latest TS version. I'm not sure at this point if the issue would occur if both the app and the codegen are using v4.7.4. When I saw the error it was with the app on the latest 4.7.4 but the codegen will load its own version fitting the specified range This is the node which triggers the error when passed to {
"pos": -1,
"end": -1,
"flags": 8,
"modifierFlagsCache": 0,
"transformFlags": 1,
"kind": 183,
"elementType": {
"pos": -1,
"end": -1,
"flags": 8,
"modifierFlagsCache": 0,
"transformFlags": 1,
"kind": 178,
"typeName": {
"pos": -1,
"end": -1,
"flags": 8,
"modifierFlagsCache": 0,
"transformFlags": 0,
"kind": 79,
"escapedText": "Pet"
}
},
"emitNode": {
"leadingComments": [
{
"kind": 3,
"pos": -1,
"end": -1,
"hasTrailingNewLine": false,
"text": "* status 200 successful operation "
}
]
}
} So we need to go up a few levels to figure out why The line that it is trying to write is: export type FindPetsByStatusApiResponse = /** status 200 successful operation */ Pet[]; It should have an array of I am not yet fully understanding which parts of the code are executed with the app TS version and which use the codegen's version, but it seems like this is a version mismatch problem. In my project's case 183 /* SyntaxKind.ArrayType */:
return emitArrayType(node);
case 184 /* SyntaxKind.TupleType */:
return emitTupleType(node); In the file where the error is triggered, case 182 /* ArrayType */:
return emitArrayType(node);
case 183 /* TupleType */:
return emitTupleType(node); |
Oh, good catch. TS versions make a lot of sense. redux-toolkit/packages/rtk-query-codegen-openapi/src/index.ts Lines 48 to 61 in b3d4d89
|
Yeah, in my project In my case this is due to how I have yarn set up, using I tried adding a resolution to force everything to use 4.7.4 and everything is working so far: "resolutions": {
"typescript": "4.7.4"
} 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. :) |
Running 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
Output
The text was updated successfully, but these errors were encountered: