{"id":11986,"library":"schema-typescript","title":"JSON Schema to TypeScript Converter","description":"This package facilitates the automatic conversion of JSON Schema definitions into TypeScript interfaces and types. It is currently at version 0.14.3 and appears to be actively maintained, with its last publish occurring four days ago as of the current date, indicating a relatively frequent release cadence. A key differentiator is its robust handling of standard JSON Schema features like `$ref` and `$defs` for modular schema definitions, and its ability to process arrays of defined types seamlessly into TypeScript arrays. Unlike some alternative tools that focus on generating JSON Schema *from* TypeScript code, this library specifically targets the generation of TypeScript *from* existing JSON Schema, providing a critical tool for schema-first development workflows. It operates with minimal dependencies and is part of the broader `constructive-io/dev-utils` monorepo, although it is designed for general-purpose use.","status":"active","version":"0.14.3","language":"javascript","source_language":"en","source_url":"https://github.com/constructive-io/dev-utils","tags":["javascript","jsonschema","schema","typescript"],"install":[{"cmd":"npm install schema-typescript","lang":"bash","label":"npm"},{"cmd":"yarn add schema-typescript","lang":"bash","label":"yarn"},{"cmd":"pnpm add schema-typescript","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses named exports. Direct CommonJS `require` might lead to `undefined` or a `TypeError` if not properly transpiled or if `esModuleInterop` is not configured, as it is designed for ESM environments.","wrong":"const generateTypeScript = require('schema-typescript');","symbol":"generateTypeScript","correct":"import { generateTypeScript } from 'schema-typescript';"}],"quickstart":{"code":"import { generateTypeScript } from 'schema-typescript';\nimport * as fs from 'node:fs/promises';\n\nconst userSchema = {\n  \"$id\": \"https://example.com/user.schema.json\",\n  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n  \"title\": \"User\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"id\": {\n      \"type\": \"string\",\n      \"format\": \"uuid\"\n    },\n    \"name\": {\n      \"type\": \"string\",\n      \"description\": \"Full name of the user\"\n    },\n    \"email\": {\n      \"type\": \"string\",\n      \"format\": \"email\"\n    },\n    \"age\": {\n      \"type\": \"integer\",\n      \"minimum\": 0,\n      \"maximum\": 150\n    },\n    \"isActive\": {\n      \"type\": \"boolean\",\n      \"default\": true\n    }\n  },\n  \"required\": [\"id\", \"name\", \"email\"]\n};\n\nasync function generateUserTypes() {\n  try {\n    const tsDefinition = await generateTypeScript(userSchema, 'User');\n    await fs.writeFile('src/types/user.d.ts', tsDefinition);\n    console.log('TypeScript types for User generated successfully to src/types/user.d.ts');\n    console.log('\\nGenerated TypeScript:\\n', tsDefinition);\n  } catch (error) {\n    console.error('Failed to generate TypeScript types:', error);\n  }\n}\n\ngenerateUserTypes();","lang":"typescript","description":"This quickstart demonstrates how to import the `generateTypeScript` function and convert a sample JSON Schema object for a 'User' into a TypeScript interface string, then write it to a file."},"warnings":[{"fix":"Always pin to exact patch versions (`~0.14.3` instead of `^0.14.3`) in `package.json` and consult release notes before upgrading to a new minor version.","message":"The package is currently in a pre-1.0 version (0.14.3). While actively maintained, this implies that breaking changes could occur in minor versions, not just major ones, unlike libraries adhering to strict semver after 1.0. Review release notes carefully during upgrades.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Test the conversion with representative complex schemas. For critical or edge-case schemas, consider simplifying the JSON Schema or manually adjusting the generated TypeScript definitions. Report specific issues to the maintainers.","message":"Complex or highly customized JSON Schema features, especially intricate `$ref` resolution across multiple files or non-standard keywords, might not be fully supported or could lead to unexpected TypeScript output.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Refer to the GitHub repository for `constructive-io/dev-utils` for the latest context and any ecosystem-specific guidelines if encountering unexpected behavior in complex integrations.","message":"As part of the `constructive-io/dev-utils` monorepo, while functional as a standalone utility, there might be implicit assumptions or optimizations tailored for the broader `constructive-io` ecosystem. This generally does not affect direct usage, but advanced users integrating deeply might encounter subtle interactions.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you are using `import { generateTypeScript } from 'schema-typescript';` in an ESM-compatible environment (e.g., Node.js with `\"type\": \"module\"` in `package.json` or a bundler configured for ESM).","cause":"Attempting to import `generateTypeScript` using CommonJS `require()` syntax in an environment that expects ESM, or incorrect named/default export handling.","error":"TypeError: generateTypeScript is not a function"},{"fix":"Validate your JSON Schema against the JSON Schema specification (e.g., using `ajv` or an online validator) before passing it to the function. Ensure all required fields like `type` are present and correctly formatted.","cause":"The input object passed to `generateTypeScript` is not a valid JSON Schema object, missing required properties, or contains syntax errors.","error":"Error: Invalid JSON Schema structure encountered"},{"fix":"Verify that all `$ref` pointers are correct and that the referenced schemas are accessible. For external `$ref`s, ensure they are correctly provided to the library if it supports such options, or pre-resolve them if necessary.","cause":"The JSON Schema contains a `$ref` that points to a definition (`#/definitions/MyType` or an external file) that either does not exist, is misspelled, or cannot be accessed/resolved by the library's internal resolver.","error":"ReferenceError: Could not resolve schema reference '#/definitions/MyType'"}],"ecosystem":"npm"}