Skip to content

Is REST API Codegen broken with TypeScript 4.8? #2565

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

Closed
Meligy opened this issue Aug 4, 2022 · 7 comments
Closed

Is REST API Codegen broken with TypeScript 4.8? #2565

Meligy opened this issue Aug 4, 2022 · 7 comments

Comments

@Meligy
Copy link

Meligy commented Aug 4, 2022

Hello,
I was playing with TypeScript 4.8, and after hitting #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?

@Meligy
Copy link
Author

Meligy commented Aug 4, 2022

I think this is the old issue I was thinking of: rtk-incubator/rtk-query-codegen#83

@Meligy
Copy link
Author

Meligy commented Aug 4, 2022

Test with typescript 4.8.0-dev.20220803

@Meligy
Copy link
Author

Meligy commented Aug 4, 2022

And works fine with typescript 4.7.4.

@phryneas
Copy link
Member

phryneas commented Aug 4, 2022

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.

@Meligy
Copy link
Author

Meligy commented Aug 5, 2022

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.

@Meligy
Copy link
Author

Meligy commented Aug 7, 2022

Are we saying that this is actually working in the PR or other places that have TS 4.8?

@Meligy
Copy link
Author

Meligy commented Aug 29, 2022

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.

@Meligy Meligy closed this as completed Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants