reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.64k stars 1.15k forks source link

rtk-query-codegen-openapi: Error Generating Code on TypeScript 5 #3124

Open Meligy opened 1 year ago

Meligy commented 1 year ago

TypeScript 5.0 beta is out, so I thought I'd try to see if my project would work on it.

And got this error from the feedback step:

node --no-warnings <PROJECT_ROOT>/node_modules/@rtk-query/codegen-openapi/lib/bin/cli.js src/tools/generate-api/openapi-config.ts
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
TypeError: Cannot create property 'pos' on string 'SOME_CLASS'
    at createNodeArray (<PROJECT_ROOT>/node_modules/typescript/lib/typescript.js:20181:17)
    at asNodeArray (<PROJECT_ROOT>/node_modules/typescript/lib/typescript.js:23661:22)
    at Object.createTypeAliasDeclaration (<PROJECT_ROOT>/node_modules/typescript/lib/typescript.js:22100:29)
    at Object.createTypeAliasDeclaration (<PROJECT_ROOT>/node_modules/@rtk-query/oazapfts-patched/src/codegen/tscodegen.ts:75:18)
    at ApiGenerator.getRefAlias (<PROJECT_ROOT>/node_modules/@rtk-query/oazapfts-patched/src/codegen/generate.ts:261:12)
    at ApiGenerator.getBaseTypeFromSchema (<PROJECT_ROOT>/node_modules/@rtk-query/oazapfts-patched/src/codegen/generate.ts:361:19)
    at ApiGenerator.getTypeFromSchema (<PROJECT_ROOT>/node_modules/@rtk-query/oazapfts-patched/src/codegen/generate.ts:346:23)
    at <PROJECT_ROOT>/node_modules/@rtk-query/oazapfts-patched/src/codegen/generate.ts:446:23
    at Array.map (<anonymous>)
    at ApiGenerator.getTypeFromProperties (<PROJECT_ROOT>/node_modules/@rtk-query/oazapfts-patched/src/codegen/generate.ts:443:58)

Where SOME_CLASS is just a model class with nothing special about it. It's not even the first class in the Swagger file.

      "SOME_CLASS": {
        "required": [
          "id",
          "isActive"
        ],
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "format": "int32"
          },
          "description": {
            "type": "string",
            "nullable": true
          },
          "code": {
            "type": "string",
            "nullable": true
          },
          "isActive": {
            "type": "boolean"
          },
          "SOMETHING_ELSE": {
            "type": "string",
            "format": "date-time",
            "nullable": true
          }
        },
        "additionalProperties": false
      },

And interestingly it does not have any property named pos or even starts with it or includes it.

Meligy commented 1 year ago

It also seems that they changed the signature of a lot of code generation APIs :(

image

Mainly removing the decorators parameter from a lot of functions that used to take it as the first parameter.

Probably related to deprecating experimental decorators in favour of EcmaScript decorators.

dbartholomae commented 1 year ago

If the maintainers need help on this, I'm happy to try to add a PR - though I might need help, so it would potentially make more sense to have a call and/or pair programming session first and then split up the work (assuming this actually is so much work that it requires multiple people to work on it).

Meligy commented 1 year ago

The same problem TypeError: Cannot create property 'pos' on string ... still exists after final release, tested with TypeScript 5.0.2.

Tamara-Sennin commented 12 months ago

Since im using Typescript 5.2.2 I need to add that the line type ReduxState = ReturnType<typeof rootReducer> doesn't produce the usual state, it's not recognising the states on the combineReducers, so when I try to use it in a selector the IDE marks it as an error. My code still compiles and works but the typescript engine on the IDE is not properly inferring types anymore. I also tried to create a reducer directly on my configureStore and then get the state from store.getState with similar results.

EskiMojo14 commented 12 months ago

@tamara-ita-scout24 this sounds like a separate issue - can you make a new issue report, ideally with a replication in code sandbox?

Tamara-Sennin commented 12 months ago

@tamara-ita-scout24 this sounds like a separate issue - can you make a new issue report, ideally with a replication in code sandbox?

The versions on code sandbox are older than the ones im using, I can't get typescript 5.2.2 nor react-redux 8.1.2. Seems to me that it could take some time for all pages to update the newer versions? For now I just reverted typescript to v4 and everything works. My package.json is this, if you'd like to know "dependencies": { "@js-boot/logger": "^3.6.0", "@paralleldrive/react-feature-toggles": "^2.3.1", "@reduxjs/toolkit": "^1.9.5", "babel-loader": "^9.1.3", "browser-process": "^0.0.1", "core-js": "^3.32.1", "cuid": "^3.0.0", "es6-promise": "4.2.8", "eslint-plugin-prettier": "^5.0.0", "history": "5.3.0", "isomorphic-fetch": "2.2.1", "prop-types": "^15.8.1", "query-string": "6.13.8", "react": "^18.2.0", "react-app-polyfill": "^3.0.0", "react-dom": "^18.1.0", "react-media": "^1.10.0", "react-redux": "^8.1.2", "react-router-dom": "^6.11.2", "url-search-params-polyfill": "^8.2.4", "web-vitals": "^3.4.0", "whatwg-fetch": "^3.6.2" }, "devDependencies": { "@js-boot/playwright": "^3.6.0", "@js-boot/react": "^3.7.0", "@svgr/webpack": "^8.1.0", "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^14.0.0", "@testing-library/user-event": "^12.0.17", "@types/jest": "^26.0.20", "@types/node": "^20.5.7", "@types/react": "^18.2.8", "@types/react-dom": "^18.2.5", "@types/react-router-dom": "^5.3.3", "@typescript-eslint/eslint-plugin": "^6.5.0", "@typescript-eslint/parser": "^6.5.0", "cross-env": "^7.0.3", "dotenv": "^16.1.4", "eslint": "^8.48.0", "husky": "^4.2.5", "jest-fetch-mock": "^3.0.3", "jest-junit": "^12.0.0", "jest-watch-typeahead": "^0.6.5", "less": "^3.11.3", "less-watch-compiler": "^1.14.6", "mini-css-extract-plugin": "^2.7.6", "minimist": "^1.2.6", "npm-run-all": "^4.1.5", "prettier": "^3.0.3", "serve": "^14.2.1", "typescript": "^4" },

And everything works just fine unless I update typescript to V5