JSON Schema to TypeScript (Forked)
The `rm-json-schema-to-typescript` package is a fork of the popular `json-schema-to-typescript` library, designed to compile JSON schemas into TypeScript interfaces and typings. Currently at version 10.2.0, this package offers modified reference resolving behavior compared to its upstream counterpart. Its development status is tied to the original project, with an explicit intention to be deprecated once the core changes implemented in this fork are accepted into the main `json-schema-to-typescript` repository. As a fork, its release cadence is likely reactive to critical fixes or its specific feature set rather than a strict schedule. It aims to provide a drop-in replacement where its altered reference resolution is beneficial, but users should be aware of its temporary nature and plan for eventual migration.
Common errors
-
Error: Cannot find module 'json-schema-to-typescript'
cause The package `rm-json-schema-to-typescript` is installed, but the import path in the code might be incorrect or the module resolver cannot find the aliased module.fixEnsure `rm-json-schema-to-typescript` is correctly installed. Based on the documentation, try `import { compile } from 'json-schema-to-typescript'` even if you installed the `rm-` prefixed package. Verify your `tsconfig.json` or module resolution settings if problems persist. -
TypeError: Schema must be an object or boolean
cause The input provided to `compile` or `compileFromFile` is not a valid JSON Schema object or a path to a valid JSON Schema file.fixValidate your JSON Schema against the JSON Schema specification (e.g., using a linter or online validator) to ensure it is correctly structured. Check the file path for `compileFromFile`. -
Error: $ref cycle detected
cause The JSON schema contains circular `$ref`erences that the parser cannot resolve, or the `cwd` option for resolving external references is incorrect.fixReview the `$ref` definitions in your JSON schema for circular dependencies. If using external `$ref`s, ensure the `cwd` option in `compile` or `compileFromFile` options correctly points to the base directory for resolution.
Warnings
- deprecated This `rm-json-schema-to-typescript` package is a fork and is explicitly slated for deprecation once its specific changes are accepted into the upstream `json-schema-to-typescript` project. Users should treat it as a temporary solution and plan for migration.
- gotcha This fork specifically features 'changed reference resolving' compared to the original `json-schema-to-typescript`. This modification may lead to different output typings for schemas that rely on complex `$ref` patterns.
- gotcha Despite the npm package name being `rm-json-schema-to-typescript`, the provided documentation and code examples suggest importing symbols from `'json-schema-to-typescript'`. This discrepancy can cause confusion and potential module resolution errors if not handled correctly.
Install
-
npm install rm-json-schema-to-typescript -
yarn add rm-json-schema-to-typescript -
pnpm add rm-json-schema-to-typescript
Imports
- compile
const { compile } = require('json-schema-to-typescript')import { compile } from 'json-schema-to-typescript' - compileFromFile
import { compileFromFile } from 'rm-json-schema-to-typescript'import { compileFromFile } from 'json-schema-to-typescript' - Options
import type { Options } from 'json-schema-to-typescript'
Quickstart
import { compile, compileFromFile } from 'json-schema-to-typescript';
import * as fs from 'fs/promises';
// Define a simple JSON schema programmatically
const mySchema = {
title: 'UserSchema',
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
name: { type: 'string' },
email: { type: 'string', format: 'email' },
isActive: { type: 'boolean', default: true }
},
required: ['id', 'name', 'email'],
additionalProperties: false
};
async function generateTypes() {
try {
// Compile from a JS object in memory
const tsFromObject = await compile(mySchema, 'UserSchema');
console.log('Generated from object:\n', tsFromObject);
// For demonstration, write to a dummy file. In real use, this might be dynamic.
await fs.writeFile('user.d.ts', tsFromObject);
// Example of compiling from a file (assuming 'example.json' exists)
// For a real scenario, you'd create this file first.
// const tsFromFile = await compileFromFile('example.json');
// await fs.writeFile('example.d.ts', tsFromFile);
console.log('TypeScript types generated successfully.');
} catch (error) {
console.error('Error generating types:', error);
}
}
generateTypes();