ts-json-schema-generator
ts-json-schema-generator copied to clipboard
UnknownNodeError processing union type from array
Hi, thanks for all the hard work on this awesome library! I wanted to report an issue that I noticed with the code below. The issue can be avoided by replacing the array elements second-to-last line with string literals.
export const blue = 'blue';
export const red = 'red';
export const yellow = 'yellow';
export type Blue = typeof blue;
export type Red = typeof red;
export type Yellow = typeof yellow;
export const colors = [blue, red, yellow] as const;
export type Colors = typeof colors[number];
It results in the following error during json-schema generation:
UnknownNodeError: Unknown node "blue
at ChainNodeParser.getNodeParser (/node_modules/ts-json-schema-generator/dist/src/src/src/ChainNodeParser.ts:47:15)
at ChainNodeParser.createType (/node_modules/ts-json-schema-generator/dist/src/src/src/ChainNodeParser.ts:32:25)
at /node_modules/ts-json-schema-generator/dist/src/NodeParser/src/src/NodeParser/ArrayLiteralExpressionNodeParser.ts:17:76
at Array.map (<anonymous>)
at ArrayLiteralExpressionNodeParser.createType (/node_modules/ts-json-schema-generator/dist/src/NodeParser/src/src/NodeParser/ArrayLiteralExpressionNodeParser.ts:17:44)
at ChainNodeParser.createType (/node_modules/ts-json-schema-generator/dist/src/src/src/ChainNodeParser.ts:32:54)
at AsExpressionNodeParser.createType (/node_modules/ts-json-schema-generator/dist/src/NodeParser/src/src/NodeParser/AsExpressionNodeParser.ts:15:37)
at ChainNodeParser.createType (/node_modules/ts-json-schema-generator/dist/src/src/src/ChainNodeParser.ts:32:54)
at TypeofNodeParser.createType (/node_modules/ts-json-schema-generator/dist/src/NodeParser/src/src/NodeParser/TypeofNodeParser.ts:31:45)
at ChainNodeParser.createType (/node_modules/ts-json-schema-generator/dist/src/src/src/ChainNodeParser.ts:32:54) {
node: IdentifierObject {
pos: 325,
end: 329,
flags: 0,
modifierFlagsCache: 0,
transformFlags: 0,
parent: NodeObject {
pos: 323,
end: 343,
flags: 0,
modifierFlagsCache: 0,
transformFlags: 0,
parent: [NodeObject],
kind: 200,
elements: [Array],
multiLine: false
},
kind: 78,
originalKeywordKind: undefined,
escapedText: 'blue',
flowNode: { flags: 2064, antecedent: [Object], node: [NodeObject] },
id: 8806
},
reference: undefined
}
Thanks for the issue report. It looks like we are not handling the case where the array has a reference to a type instead of a literal.
just bumping this;
i also have :
const Cultures = {"English": "en-US", ...}
and tried for :
const defaultLocales = [Cultures.English, Cultures.German, Cultures.Spanish, Cultures.FrenchCanadien] as const;
and getting error: "Uknown Node 'Cultures.English' "
i don't get error if i express as strings:
const defaultLocales = ["en-US", "de-DE", "es-ES", "fr-CA"] as const;
anyway i can typecast this?