Registry / devops / ts-transform-json-schema

ts-transform-json-schema

JSON →
library2.0.3jsnpmunverified

ts-transform-json-schema (v2.0.3) is a TypeScript custom transformer that generates inline JSON Schema from TypeScript interfaces and types at compile time. It replaces type annotations with actual schema objects, eliminating the need for runtime reflection. The library integrates with TTypeScript and supports schema options like additionalProperties. It is maintained sparsely, with the latest version from February 2021. Key differentiators: compile-time transformation vs runtime generators, zero runtime cost, and direct TypeScript type source of truth.

npm install ts-transform-json-schema
INSTALL
IMPORT
SIG · TS-TRANSFORM-JSON-
T
ts-transform-json-schema
devopsjavascriptv2.0.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fromType
import { fromType } from 'ts-transform-json-schema'
import JsonSchema from 'ts-transform-json-schema' // Non-named import doesn't work for fromType
fromType is a named export. Default export is not available.
JsonSchema (namespace)
import * as JsonSchema from 'ts-transform-json-schema'
const JsonSchema = require('ts-transform-json-schema') // CommonJS require not supported in transformers
Namespace import for use with JsonSchema.fromType() pattern.
types only (TS interface)
import type { SomeInterface } from './types'
import { SomeInterface } from './types' // Can cause runtime import if not using type-only
Interfaces used only in generics should be imported with type-only imports to avoid runtime overhead.

Configures TTypeScript and demonstrates compile-time JSON Schema generation from a TypeScript interface.

// tsconfig.json { "compilerOptions": { "target": "es2015", "module": "commonjs", "plugins": [ { "transform": "ts-transform-json-schema", "type": "program" } ] }, "include": ["src"] } // src/index.ts import { fromType } from 'ts-transform-json-schema'; interface User { id: number; name: string; email?: string; } export const userSchema = fromType<User>({ additionalProperties: false }); // Compiled output (after ttsc): // const userSchema = { // type: 'object', // properties: { // id: { type: 'number' }, // name: { type: 'string' }, // email: { type: 'string' } // }, // required: ['id', 'name'], // additionalProperties: false, // $schema: 'http://json-schema.org/draft-07/schema#' // };
Debug
Known issues
gotchaRequires TTypeScript: ts-transform-json-schema is a TypeScript custom transformer and cannot be used with standard tsc.
fix
Install and use ttypescript: npm install ttypescript --save-dev and run ttsc instead of tsc.
affects: >=0.0.0
gotchaUnsupported types: Complex types like union types, intersection types, generics, and mapped types may not be fully supported or produce incorrect schema.
fix
Stick to simple interfaces and type aliases. Test generated schema for complex types.
affects: >=0.0.0
breakingBreaking change in v2: The fromType signature changed; options object now accepts additionalProperties etc. directly.
fix
Update calls from fromType<SomeInterface>(false) to fromType<SomeInterface>({ additionalProperties: false })
affects: >=2.0.0
deprecatedDraft 07 only: The generated schema uses JSON Schema draft-07. Draft 2020-12 is not supported.
fix
No fix; library does not support other drafts. Consider alternatives if newer draft required.
affects: >=0.0.0
Errors
Common errors & fixes
error TS2339: Property 'fromType' does not exist on type 'typeof import("ts-transform-json-schema")'
Using namespace import without enabling the transformer in tsconfig.json.
fix
Add ts-transform-json-schema to compilerOptions.plugins in tsconfig.json and use ttsc.
TypeError: (0 , ts_transform_json_schema_1.fromType) is not a function
Running with tsc instead of ttsc. The transformer is not applied.
fix
Use ttsc command from ttypescript package: npx ttsc
Cannot find module 'ts-transform-json-schema'
Package not installed, or using ts-node without tsconfig-paths.
fix
Run npm install ts-transform-json-schema --save-dev and ensure tsconfig.json has the plugin entry.
Upgrade
Version history
2.0.3latest on npm
Audit
Dependencies
ttypescriptrequiredRequired to run TypeScript custom transformers
Agent activity
6 hits · last 30 days
node
6
Resources
ts-transform-json-schema — npm install ts-transform-json-schema · libregistry