vega / ts-json-schema-generator

Generate JSON schema from your Typescript sources
MIT License
1.44k stars 190 forks source link

Schema generation fails if TS file has references to Typescript's built-in types #1571

Open jrencz opened 1 year ago

jrencz commented 1 year ago

In one of my types I have references to native interfaces that come with typescript, let's say:

export type MyForm = {
  action: HTMLFormElement['action'],
  method: HTMLFormElement['method'],
}

(Type is a bit more complex, but I boiled it down to parts that cause problem)

When I try to generate schema out of this file I get the following error:

./node_modules/ts-json-schema-generator/dist/ts-json-schema-generator.js:95
        throw error;
        ^

TypeError: Cannot read properties of undefined (reading 'kind')
    at Object.isEnumDeclaration (./node_modules/ts-json-schema-generator/node_modules/typescript/lib/typescript.js:29629:21)
    at TypeofNodeParser.createType (./node_modules/ts-json-schema-generator/dist/src/NodeParser/TypeofNodeParser.js:28:34)
    at ChainNodeParser.createType (./node_modules/ts-json-schema-generator/dist/src/ChainNodeParser.js:28:54)
    at ./node_modules/ts-json-schema-generator/dist/src/NodeParser/IntersectionNodeParser.js:24:72
    at Array.map (<anonymous>)
    at IntersectionNodeParser.createType (./node_modules/ts-json-schema-generator/dist/src/NodeParser/IntersectionNodeParser.js:24:34)
    at ChainNodeParser.createType (./node_modules/ts-json-schema-generator/dist/src/ChainNodeParser.js:28:54)
    at AnnotatedNodeParser.createType (./node_modules/ts-json-schema-generator/dist/src/NodeParser/AnnotatedNodeParser.js:30:47)
    at ./node_modules/ts-json-schema-generator/dist/src/NodeParser/InterfaceAndClassNodeParser.js:103:134
    at Array.map (<anonymous>)

If I replace the references to HTMLFormElement with their actual values (in both cases it's string) I get the correct schema:

{
  "$ref": "#/definitions/MyForm",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "MyForm": {
      "additionalProperties": false,
      "properties": {
        "action": {
          "type": "string"
        },
        "method": {
          "type": "string"
        }
      },
      "required": [
        "action",
        "method"
      ],
      "type": "object"
    }
  }
}

I also get a correct schema when I replicate the part of HTMLFormElement in module exporting type MyForm:

interface HTMLFormElement {
  action: string
  method: string
}

export type MyForm = {
  action: HTMLFormElement['action'],
  method: HTMLFormElement['method'],
}

It's (AFAIK) impossible to import those built-in types explicitly

pjhenning commented 1 year ago

I think I'm running in to this issue as well.

JumpLink commented 1 year ago

I get a similar error message, apparently it is related to enums:

TypeError: Cannot read properties of undefined (reading 'kind')
    at Object.isEnumDeclaration (./.yarn/cache/typescript-patch-97a611e52a-f5481fa3ba.zip/node_modules/typescript/lib/typescript.js:26316:17)
    at TypeofNodeParser.createType (./.yarn/cache/ts-json-schema-generator-npm-1.3.0-e61e0429c2-4d4ca009a3.zip/node_modules/ts-json-schema-generator/dist/src/NodeParser/TypeofNodeParser.js:28:34)
    at ChainNodeParser.createType (./.yarn/cache/ts-json-schema-generator-npm-1.3.0-e61e0429c2-4d4ca009a3.zip/node_modules/ts-json-schema-generator/dist/src/ChainNodeParser.js:28:54)
    at ./.yarn/cache/ts-json-schema-generator-npm-1.3.0-e61e0429c2-4d4ca009a3.zip/node_modules/ts-json-schema-generator/dist/src/NodeParser/IntersectionNodeParser.js:24:72
    at Array.map (<anonymous>)
    at IntersectionNodeParser.createType (./.yarn/cache/ts-json-schema-generator-npm-1.3.0-e61e0429c2-4d4ca009a3.zip/node_modules/ts-json-schema-generator/dist/src/NodeParser/IntersectionNodeParser.js:24:34)
    at ChainNodeParser.createType (./.yarn/cache/ts-json-schema-generator-npm-1.3.0-e61e0429c2-4d4ca009a3.zip/node_modules/ts-json-schema-generator/dist/src/ChainNodeParser.js:28:54)
    at AnnotatedNodeParser.createType (./.yarn/cache/ts-json-schema-generator-npm-1.3.0-e61e0429c2-4d4ca009a3.zip/node_modules/ts-json-schema-generator/dist/src/NodeParser/AnnotatedNodeParser.js:30:47)
    at ./.yarn/cache/ts-json-schema-generator-npm-1.3.0-e61e0429c2-4d4ca009a3.zip/node_modules/ts-json-schema-generator/dist/src/NodeParser/InterfaceAndClassNodeParser.js:103:134
    at Array.map (<anonymous>)
TheUltDev commented 11 months ago

Also running into this issue when generating a type that uses Microsoft's Monaco Editor's types since it references HTMLElement.

arthurfiorette commented 4 months ago

Thanks for the bug report. Would you like to start a PR? I'm happy to review it

jrencz commented 4 months ago

Hi,

I actually phased out the ts type->jsonschema approach in my project in favour of metamodel->(jsonschema, ts type) with Typebox.

That approch is a bit more complex, but when my project started scaling up I observed not only behavior like one reported here, but also generation times increasing. Since I do own the types and I can just rewrite them, I decided to do so. It may not be an option everywhere

arthurfiorette commented 4 months ago

Probably this issue could be resolved by just adding --tsconfig ./tsconfig.json to correctly load all types.

I'll further investigate this once I have time.