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

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

Open jrencz opened this issue 2 years ago • 6 comments

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

jrencz avatar Feb 17 '23 14:02 jrencz

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

pjhenning avatar Aug 03 '23 21:08 pjhenning

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>)

JumpLink avatar Sep 04 '23 11:09 JumpLink

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

TheUltDev avatar Oct 25 '23 04:10 TheUltDev