ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

Function with generic keyof param crashes

Open LeviticusMB opened this issue 2 years ago • 1 comments

I'm upgrading from 0.97.0 to 1.3.0 and now ts-json-schema-generator crashes. I have a schema.ts with exported types plus a helper function for validating the types using the generated schema.

😀 mb@bia:test$ npx ts-json-schema-generator --path schema.ts 
/Users/mb/.npm/_npx/dec19a6f9609cbed/node_modules/ts-json-schema-generator/dist/ts-json-schema-generator.js:95
        throw error;
        ^

TypeError: Cannot read properties of undefined (reading 'getId')
    at /Users/mb/.npm/_npx/dec19a6f9609cbed/node_modules/ts-json-schema-generator/dist/src/NodeParser/IndexedAccessTypeNodeParser.js:54:76
    at Array.map (<anonymous>)
    at IndexedAccessTypeNodeParser.createType (/Users/mb/.npm/_npx/dec19a6f9609cbed/node_modules/ts-json-schema-generator/dist/src/NodeParser/IndexedAccessTypeNodeParser.js:52:42)
    at ChainNodeParser.createType (/Users/mb/.npm/_npx/dec19a6f9609cbed/node_modules/ts-json-schema-generator/dist/src/ChainNodeParser.js:28:54)
    at ParameterParser.createType (/Users/mb/.npm/_npx/dec19a6f9609cbed/node_modules/ts-json-schema-generator/dist/src/NodeParser/ParameterParser.js:16:37)
    at AnnotatedNodeParser.createType (/Users/mb/.npm/_npx/dec19a6f9609cbed/node_modules/ts-json-schema-generator/dist/src/NodeParser/AnnotatedNodeParser.js:30:47)
    at ChainNodeParser.createType (/Users/mb/.npm/_npx/dec19a6f9609cbed/node_modules/ts-json-schema-generator/dist/src/ChainNodeParser.js:28:54)
    at /Users/mb/.npm/_npx/dec19a6f9609cbed/node_modules/ts-json-schema-generator/dist/src/NodeParser/FunctionParser.js:24:41
    at Array.map (<anonymous>)
    at FunctionParser.createType (/Users/mb/.npm/_npx/dec19a6f9609cbed/node_modules/ts-json-schema-generator/dist/src/NodeParser/FunctionParser.js:23:48)
👿 mb@bia:test$ 

schema.ts contains:

export interface A {
    aProp: string;
}

export interface B {
    bProp: number;
}

interface Objects {
    'A': A,
    'B': B,
}

export function validate<K extends keyof Objects>(type: K, object: Objects[K]): Objects[K] {
    // Actual validation using generated schema and AJV here
    return object;
}

It looks like it broke in between 1.0.0 and 1.1.0.

LeviticusMB avatar Aug 30 '23 12:08 LeviticusMB

BTW a workaround for my issue would be to add /** @hidden */ to validate() (#330), but still ...

LeviticusMB avatar Aug 30 '23 12:08 LeviticusMB