Skip to content
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

Recursive types crashes #1861

Open
loucadufault opened this issue Feb 1, 2024 · 6 comments
Open

Recursive types crashes #1861

loucadufault opened this issue Feb 1, 2024 · 6 comments

Comments

@loucadufault
Copy link

loucadufault commented Feb 1, 2024

Encountered with this repro:

npx ts-json-schema-generator --path 'schema.ts' --type 'Schema'

/workspaces/my-app/node_modules/ts-json-schema-generator/dist/ts-json-schema-generator.js:99
        throw error;
        ^

TypeError: Cannot read properties of undefined (reading 'getId')
    at /workspaces/my-app/node_modules/ts-json-schema-generator/dist/src/Utils/nodeKey.js:43:57
    at Array.map ()
    at getKey (/workspaces/my-app/node_modules/ts-json-schema-generator/dist/src/Utils/nodeKey.js:43:40)
    at Context.getCacheKey (/workspaces/my-app/node_modules/ts-json-schema-generator/dist/src/NodeParser.js:30:55)
    at ChainNodeParser.createType (/workspaces/my-app/node_modules/ts-json-schema-generator/dist/src/ChainNodeParser.js:25:41)
    at TypeReferenceNodeParser.createType (/workspaces/my-app/node_modules/ts-json-schema-generator/dist/src/NodeParser/TypeReferenceNodeParser.js:55:37)
    at ChainNodeParser.createType (/workspaces/my-app/node_modules/ts-json-schema-generator/dist/src/ChainNodeParser.js:28:54)
    at TypeReferenceNodeParser.createSubContext (/workspaces/my-app/node_modules/ts-json-schema-generator/dist/src/NodeParser/TypeReferenceNodeParser.js:62:62)
    at TypeReferenceNodeParser.createType (/workspaces/my-app/node_modules/ts-json-schema-generator/dist/src/NodeParser/TypeReferenceNodeParser.js:55:118)
    at ChainNodeParser.createType (/workspaces/my-app/node_modules/ts-json-schema-generator/dist/src/ChainNodeParser.js:28:54)

Node.js v20.9.0

schema.ts

import { Options } from 'sequelize'

export interface Schema {
  sequelize: Pick<Options, 'host'>
}   

package.json

"sequelize": "^6.35.2"

Investigating, the culprit seems to be this path in Options interface from sequelize:

Options['define']['defaultScope']['where']

Which is the recursive AllowNotOrAndWithImplicitAndArrayRecursive type, see https://github.com/sequelize/sequelize/blob/b8212434b89e1622f86d1f8e1ea1a5daa060cea5/packages/core/src/dialects/abstract/where-sql-builder-types.ts#L17.

@loucadufault
Copy link
Author

loucadufault commented Feb 1, 2024

Also ran tsc, which is working with no errors.

@domoritz
Copy link
Member

domoritz commented Feb 1, 2024

Thanks for the report. Can you provide a minimal reproducible example that demonstrates the issue?

@loucadufault
Copy link
Author

@domoritz Anything specifically missing from the reproducible example provided in the ticket?

@domoritz
Copy link
Member

domoritz commented Feb 2, 2024

It's not minimal. The smaller, the easier it will be to identify whether this is a knows issue or a new issue and how to fix it.

@loucadufault
Copy link
Author

Considering it is a total of 4 LoC, how do you propose I minimize this further?

@domoritz
Copy link
Member

domoritz commented Feb 2, 2024

It's referring to some extra code. I tried to extract the code for AllowNotOrAndWithImplicitAndArrayRecursive and it seems to work.

type AllowArray<T> = T | T[];

interface OpTypes {
    readonly and: unique symbol;
    readonly or: unique symbol;
    readonly not: unique symbol;
}

const Op: OpTypes = {
    and: Symbol.for("and"),
    or: Symbol.for("or"),
    not: Symbol.for("not"),
} as OpTypes;

type AllowNotOrAndWithImplicitAndArrayRecursive<T> = AllowArray<
    | T
    | { [Op.or]: AllowArray<AllowNotOrAndWithImplicitAndArrayRecursive<T>> }
    | { [Op.and]: AllowArray<AllowNotOrAndWithImplicitAndArrayRecursive<T>> }
    | { [Op.not]: AllowNotOrAndWithImplicitAndArrayRecursive<T> }
>;

export type Test = AllowNotOrAndWithImplicitAndArrayRecursive<number>;

I ran yarn --silent run run --path 'test.ts' --type 'Test'

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