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

UnknownNodeError processing union type from array

Open mdesousa opened this issue 3 years ago • 2 comments

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
}

mdesousa avatar Jun 24 '21 12:06 mdesousa

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.

domoritz avatar Jun 24 '21 15:06 domoritz

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?

Kiel-H-Byrne avatar Oct 25 '21 19:10 Kiel-H-Byrne