keajs/kea-typegen

Bug generating types with selectors

Closed this issue · 5 comments

Bug description

I'm probably missing something basic, but this logic works 100% as expected when running kea-typegen write:

export const authLogic = kea<authLogicType>({
  loaders: {
    rawToken: [
      null as string | null,
      {
        loadToken: async (payload) => {
          const response = await api.post("/auth", payload);
          return response.data;
        },
      },
    ],
  },
});

but strangely, when adding a simple selector, kea-typegen stops working:

export const authLogic = kea<authLogicType>({
  loaders: {
    rawToken: [
      null as string | null,
      {
        loadToken: async (payload) => {
          const response = await api.post("/login", payload);
          return response.data;
        },
      },
    ],
  },
  selectors: {
    token: [
      (s) => [s.rawToken],
      (rawToken: string | null) => {
        return rawToken;
      },
    ],
  },
});

leads to the following error when running kea-typegen write:

Using TypeScript Config: [path]/tsconfig.json

[path]/node_modules/kea-typegen/node_modules/yargs/build/lib/yargs.js:1132
                throw err;
                ^

TypeError: Cannot read property 'map' of undefined
    at Object.visitSelectors [as selectors] ([path]/node_modules/kea-typegen/dist/visit/visitSelectors.js:19:68)
    at visit ([path]/node_modules/kea-typegen/dist/visit/visit.js:102:81)
    at visitNode ([path]/node_modules/typescript/lib/typescript.js:27849:24)
    at Object.forEachChild ([path]/node_modules/typescript/lib/typescript.js:28045:24)
    at visit ([path]/node_modules/kea-typegen/dist/visit/visit.js:55:16)
    at visitNode ([path]/node_modules/typescript/lib/typescript.js:27849:24)
    at Object.forEachChild ([path]/node_modules/typescript/lib/typescript.js:27943:21)
    at visit ([path]/node_modules/kea-typegen/dist/visit/visit.js:55:16)
    at visitNodes ([path]/node_modules/typescript/lib/typescript.js:27858:30)
    at Object.forEachChild ([path]/node_modules/typescript/lib/typescript.js:28104:24)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

How to reproduce

See details above.

Expected behavior

kea-typegen should generate the types properly even when using selectors.

Environment

  • kea-typegen version: 0.3.5
  • kea version: 2.2.2
  • typescript version: 4.1.3

Additional context

Base tsconfig.json from https://github.com/facebook/create-react-app

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": ["src"]
}

Hey! Please try with 0.4.0. It seems TS 4.1 (or even 4.0?) changed some things making the previous code fail.

Thanks for the quick reply @mariusandra! I upgraded kea-typegen to 0.4.0 but same outcome. Tried downgrading typescript to 4.0 and same thing. Downgrading typescript to 3.9.4 did the trick. Whenever support for typescript >= 4.0 happens, would you mind updating this issue?

Hey, very weird. I must have messed something up when publishing a new version. kea-typegen 0.4.1 should work with TS 4.1. I just tested on a new project and it worked.

Works as expected! Thanks for the super quick help here 😁

Documenting in case it helps someone.

Just had this problem with Kea v0.5.1 - upgrading to v0.5.3 solved it. Took me quite a long time thinking I was messing up my selectors 😬

TS version was 4.1.5