redux-toolkit
redux-toolkit copied to clipboard
Is REST API Codegen broken with TypeScript 4.8?
Hello, I was playing with TypeScript 4.8, and after hitting https://github.com/reduxjs/redux-toolkit/pull/2547 and working around it, it seemed that code generation also failed.
I found that I was still using @rtk-incubator/rtk-query-codegen-openapi, which is no longer mentioned in the docs, instead, the docs recommend using @rtk-query/codegen-openapi, so I switched to it, but the same problem happened:
Please note that `@rtk-query/codegen-openapi` only has been tested with TS versions 4.1 to 4.5 - other versions might cause problems.
Generating ../../../src/generated/api.ts
SyntaxError: Identifier expected. (1:23)
> 1 | import { type api as } from "../tools/generated-api/base-api";
| ^
2 | const injectedRtkApi = api.injectEndpoints({
3 | endpoints: build => ({
4 | get___: build.query<___ApiResponse, Get___ApiArg>({
at Ve (REDACTED_BASE_PATH/node_modules/prettier/parser-typescript.js:1:15607)
at vz (REDACTED_BASE_PATH/node_modules/prettier/parser-typescript.js:280:5919)
at Object.yz [as parse] (REDACTED_BASE_PATH/node_modules/prettier/parser-typescript.js:280:6242)
at Object.parse (REDACTED_BASE_PATH/node_modules/prettier/index.js:7334:23)
at coreFormat (REDACTED_BASE_PATH/node_modules/prettier/index.js:8645:18)
at formatWithCursor2 (REDACTED_BASE_PATH/node_modules/prettier/index.js:8837:18)
at REDACTED_BASE_PATH/node_modules/prettier/index.js:37229:12
at Object.format (REDACTED_BASE_PATH/node_modules/prettier/index.js:37243:12)
at prettify (REDACTED_BASE_PATH/node_modules/@rtk-query/codegen-openapi/src/utils/prettier.ts:35:19)
at async generateEndpoints (REDACTED_BASE_PATH/node_modules/@rtk-query/codegen-openapi/src/index.ts:20:63) {
loc: { start: { line: 1, column: 23 } },
codeFrame: '\x1B[0m\x1B[31m\x1B[1m>\x1B[22m\x1B[39m\x1B[90m 1 |\x1B[39m \x1B[36mimport\x1B[39m { type api \x1B[36mas\x1B[39m } \x1B[36mfrom\x1B[39m \x1B[32m"../tools/generated-api/base-api"\x1B[39m\x1B[33m;\x1B[39m\x1B[0m\n' +
'\x1B[0m \x1B[90m |\x1B[39m \x1B[31m\x1B[1m^\x1B[22m\x1B[39m\x1B[0m\n' +
'\x1B[0m \x1B[90m 2 |\x1B[39m \x1B[36mconst\x1B[39m injectedRtkApi \x1B[33m=\x1B[39m api\x1B[33m.\x1B[39minjectEndpoints({\x1B[0m\n' +
'\x1B[0m \x1B[90m 3 |\x1B[39m endpoints\x1B[33m:\x1B[39m build \x1B[33m=>\x1B[39m ({\x1B[0m\n' +
'\x1B[0m \x1B[90m 4 |\x1B[39m get___\x1B[33m:\x1B[39m build\x1B[33m.\x1B[39mquery\x1B[33m<\x1B[39m\x1B[33mGet___ApiResponse\x1B[39m\x1B[33m,\x1B[39m \x1B[33mGet___ApiArg\x1B[39m\x1B[33m>\x1B[39m({\x1B[0m'
}
(The parts replaced with ___ are specific API endpoint names)
Note this bit
> 1 | import { type api as } from "../tools/generated-api/base-api";
| ^
The missing alias is there both in the @rtk-incubator/rtk-query-codegen-openapi version that does not provide an API at all (only a custom query), and in the @rtk-query/codegen-openapi version no matter what I make the config values (I tried making the exportName of the API file become api, or something else other than api, and the result is the same).
I'm sure it happened in the past due to some dependency issue, but I cannot remember the details.
Is anyone else seeing this?
I think this is the old issue I was thinking of: https://github.com/rtk-incubator/rtk-query-codegen/issues/83
Test with typescript 4.8.0-dev.20220803
And works fine with typescript 4.7.4.
See #2425. It's very important that you only have one version of TypeScript in your project and there is not a trace of type mismatch between the types used by oazapfts and the codegen or things will break.
Hi @phryneas.
Yes, I only have one version of TS in my project. It is enforced by the overrides property in my package.json, and confirmed by npm ls typescript.
Are we saying that this is actually working in the PR or other places that have TS 4.8?
I'm not seeing this issue with TS 4.8 final (tested with 4.8.2), so, closing this issue.
Thanks a lot for reply here and on Twitter.