ts-json-schema-generator
ts-json-schema-generator copied to clipboard
fails to parse types from declaration files (`*.d.ts`)
Summary
ts-json-schema-generator fails to parse types from declaration files (*.d.ts).
Repro
With the following minimal repro:
// declaration.d.ts
declare type Model = string;
// ForgeElementOverlay.ts
export interface ForgeElementOverlay {
model: Model;
}
// main.ts
import { createGenerator } from './factory/generator';
const config = {
path: "ForgeElementOverlay.ts",
skipTypeCheck: true,
tsconfig: "tsconfig.json",
type: "*",
};
const schema = createGenerator(config).createSchema(config.type);
where tsconfig.json is identical to the one used by the ts-json-schema-generator package:
https://github.com/vega/ts-json-schema-generator/blob/next/tsconfig.json
and main.ts is run with Bun v1.1.42
The following error is thrown:
~/src/ts-json-schema-generator [next]
» bun run main.ts
19 | */
20 | export abstract class BaseError extends Error {
21 | readonly diagnostic: ts.Diagnostic;
22 |
23 | constructor(diagnostic: PartialDiagnostic) {
24 | super(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
^
error: Unhandled error while creating Base Type.
at new BaseError (/Users/robcmills/src/ts-json-schema-generator/src/Error/BaseError.ts:24:9)
at new UnhandledError (/Users/robcmills/src/ts-json-schema-generator/src/Error/Errors.ts:112:9)
at from (/Users/robcmills/src/ts-json-schema-generator/src/Error/Errors.ts:121:53)
at createType (/Users/robcmills/src/ts-json-schema-generator/src/ChainNodeParser.ts:39:38)
at createType (/Users/robcmills/src/ts-json-schema-generator/src/NodeParser/AnnotatedNodeParser.ts:34:47)
at /Users/robcmills/src/ts-json-schema-generator/src/NodeParser/InterfaceAndClassNodeParser.ts:146:46
at map (1:11)
at getProperties (/Users/robcmills/src/ts-json-schema-generator/src/NodeParser/InterfaceAndClassNodeParser.ts:142:14)
at createType (/Users/robcmills/src/ts-json-schema-generator/src/NodeParser/InterfaceAndClassNodeParser.ts:48:33)
at createType (/Users/robcmills/src/ts-json-schema-generator/src/NodeParser/AnnotatedNodeParser.ts:34:47)
76 | if (typeSymbol.name === "URL") {
77 | return new AnnotatedType(new StringType(), { format: "uri" }, false);
78 | }
79 |
80 | return this.childNodeParser.createType(
81 | typeSymbol.declarations!.filter((n: ts.Declaration) => !invalidTypes[n.kind])[0],
^
TypeError: undefined is not an object (evaluating 'typeSymbol.declarations.filter')
at createType (/Users/robcmills/src/ts-json-schema-generator/src/NodeParser/TypeReferenceNodeParser.ts:81:24)
at createType (/Users/robcmills/src/ts-json-schema-generator/src/ChainNodeParser.ts:37:49)
at createType (/Users/robcmills/src/ts-json-schema-generator/src/NodeParser/AnnotatedNodeParser.ts:34:47)
at /Users/robcmills/src/ts-json-schema-generator/src/NodeParser/InterfaceAndClassNodeParser.ts:146:46
at map (1:11)
at getProperties (/Users/robcmills/src/ts-json-schema-generator/src/NodeParser/InterfaceAndClassNodeParser.ts:142:14)
at createType (/Users/robcmills/src/ts-json-schema-generator/src/NodeParser/InterfaceAndClassNodeParser.ts:48:33)
at createType (/Users/robcmills/src/ts-json-schema-generator/src/NodeParser/AnnotatedNodeParser.ts:34:47)
at createType (/Users/robcmills/src/ts-json-schema-generator/src/ExposeNodeParser.ts:23:45)
at createType (/Users/robcmills/src/ts-json-schema-generator/src/CircularReferenceNodeParser.ts:24:43)
Bun v1.1.42 (macOS arm64)
On the surface, I found this is because typeSymbol.declarations is undefined, for the declared Model type.
I haven't dug any deeper than this.
Additional info
Of note, typescript-json-schema handles this case without error, however the generated schema for the declared type is empty:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Model": {},
"ForgeElementOverlay": {
"type": "object",
"properties": {
"model": {
"$ref": "ospace#/definitions/Model"
},
},
"required": [
"model",
]
},
},
}
This would be a sufficient workaround for our use case.