https://github.com/cyansalt/transchema
Transform JSON Schema definition into inline TypeScript type
https://github.com/cyansalt/transchema
json-schema typescript
Last synced: 4 months ago
JSON representation
Transform JSON Schema definition into inline TypeScript type
- Host: GitHub
- URL: https://github.com/cyansalt/transchema
- Owner: CyanSalt
- Created: 2025-05-29T03:48:53.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-11-28T02:33:01.000Z (7 months ago)
- Last Synced: 2025-11-30T14:55:47.274Z (7 months ago)
- Topics: json-schema, typescript
- Language: TypeScript
- Homepage:
- Size: 278 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# transchema
[](https://www.npmjs.com/package/transchema)
Transform JSON Schema definition into inline TypeScript type.
## Usage
```ts
import { transform } from 'transchema'
transform({
type: 'array',
prefixItems: [
{ type: 'number' },
{ type: 'string' },
{ enum: ['Street', 'Avenue', 'Boulevard'] },
{ enum: ['NW', 'NE', 'SW', 'SE'] },
],
items: { type: 'string' },
}) // [number, string, 'Street' | 'Avenue' | 'Boulevard', 'NW' | 'NE' | 'SW' | 'SE', ...string[]]
```
`transform` supports the following options:
- `additionalProperties`: `additionalProperties` for objects defaults to `true` in the JSON Schema specification, which means that all object types are non-closed. In this case, transchema will add `& Record` union types to these objects to ensure semantic consistency. You can change its default value by setting `additionalProperties: false` to reduce the appearance of this type.
## Why use it?
The typical scenario is to display type of a JSON Schema in the browser. This is useful for displaying MCP tool parameters.
Unlike other Json-Schema-to-TypeScript tools, it has:
- 0 runtime dependencies, so it's safe to run in the browser.
- Inline types generation, which will render human-readable types.
As such, modular combination features such as `$ref` are not supported.
## Other Tools
- If you want to transform JSON Schema to a complete TypeScript declaration file, you can use [`json-schema-to-typescript-lite`](https://github.com/antfu/json-schema-to-typescript-lite).
- If you use `zod` as the schema describer, [`zod-to-ts`](https://github.com/sachinraja/zod-to-ts) supports generating inline types.