typescript-json-schema is a library designed to generate JSON Schema definitions directly from TypeScript source files. It operates by compiling the TypeScript program to extract comprehensive type information and then translating this into JSON Schema, including features like required properties, inheritance (`extends`), JSDoc annotation keywords, and default values from property initializers. The current stable version is 0.67.1. The project is explicitly stated to be in "maintenance mode", indicating a slower release cadence and less active feature development compared to its past. While lightweight, the maintainers suggest `ts-json-schema-generator` for a more feature-rich and actively developed alternative, highlighting `typescript-json-schema`'s primary differentiator as its use of the TypeScript compiler internally, enabling advanced scenarios.
npm install typescript-json-schemaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates programmatic JSON Schema generation from a TypeScript interface using temporary files for demonstration. It configures the generator with common settings like `required` properties and `strictNullChecks` to produce a comprehensive schema, then prints it to the console.
Evaluate alternatives like `ts-json-schema-generator` for projects requiring active development or advanced features.
Always use `path.resolve` for file paths and verify your `tsconfig.json` or `compilerOptions` align with your project setup. Ensure the `basePath` parameter is correctly set, often to `__dirname` or the project root.
Set `noExtraProps: true` in your generator settings if you want objects to disallow properties not explicitly defined in the TypeScript interface/type.
Refer to the library's API documentation for the correct JSDoc annotation syntax, especially for overriding inferred types or adding custom keywords.
Verify the absolute path to your `tsconfig.json` or `.ts` files. Use `path.resolve()` for robustness. Ensure the files are accessible from where the script is run.
Double-check the type name for typos and ensure the type is correctly defined and exported in the TypeScript files included in the `getProgramFromFiles` call.
Inspect the `program` object returned by `TJS.getProgramFromFiles` for errors. Ensure `filePaths` and `compilerOptions` are valid. Log the `generator` to see if it was successfully created.