Generate TypeScript types from JSON Schema with json-schema-to-typescript: working versions (tested 2026-10-15)
Compile JSON Schemas into TypeScript interfaces with json-schema-to-typescript, covering required and optional fields, enums, $ref definitions, descriptions as JSDoc and strict additionalProperties.
passed4 passed · 0 failed2026-10-15 17:10 UTC- Status
- passed
- Last passed
- 2026-10-15 17:10 UTC
- Tests
- 4 / 4
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
json-schema-to-typescript@16.0.0@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38775053802
The code
Exactly the files that passed. Your agent can pull them with get_recipe("json-schema-to-typescript-codegen") (install), or see them on GitHub.
package.json
{
"name": "json-schema-to-typescript-codegen",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"json-schema-to-typescript": "16.0.0"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/codegen.ts
import { compile, type JSONSchema } from "json-schema-to-typescript";
export const orderSchema: JSONSchema = {
title: "Order",
type: "object",
additionalProperties: false,
required: ["id", "status", "items"],
properties: {
id: { type: "string", description: "Order id, e.g. ord_123" },
status: { type: "string", enum: ["pending", "paid", "shipped"] },
note: { type: "string" },
items: { type: "array", items: { $ref: "#/definitions/LineItem" }, minItems: 1 },
},
definitions: {
LineItem: {
type: "object",
additionalProperties: false,
required: ["sku", "qty"],
properties: { sku: { type: "string" }, qty: { type: "integer", minimum: 1 } },
},
},
};
export async function generateTypes(schema: JSONSchema, name: string): Promise<string> {
return compile(schema, name, {
bannerComment: "/* Generated from JSON Schema. Do not edit. */",
additionalProperties: false,
strictIndexSignatures: true,
style: { singleQuote: false, semi: true },
});
}
test/codegen.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { generateTypes, orderSchema } from "../src/codegen.ts";
test("generates interfaces with required, optional and enum fields", async () => {
const ts = await generateTypes(orderSchema, "Order");
assert.match(ts, /^\/\* Generated from JSON Schema\. Do not edit\. \*\//);
assert.match(ts, /export interface Order \{/);
assert.match(ts, /\n\s+id: string;/);
assert.match(ts, /status: "pending" \| "paid" \| "shipped";/);
assert.match(ts, /note\?: string;/);
assert.match(ts, /items: \[LineItem, \.\.\.LineItem\[\]\];/);
});
test("$ref definitions become their own interfaces", async () => {
const ts = await generateTypes(orderSchema, "Order");
assert.match(ts, /export interface LineItem \{/);
assert.match(ts, /sku: string;/);
assert.match(ts, /qty: number;/);
assert.doesNotMatch(ts, /\[k: string\]: unknown/);
});
test("descriptions become JSDoc comments", async () => {
const ts = await generateTypes(orderSchema, "Order");
assert.match(ts, /\/\*\*\n\s+\* Order id, e\.g\. ord_123\n\s+\*\/\n\s+id: string;/);
});
test("open objects keep an index signature when allowed", async () => {
const ts = await generateTypes({ title: "Meta", type: "object", additionalProperties: true, properties: { a: { type: "number" } } }, "Meta");
// strictIndexSignatures adds "| undefined" so lookups by unknown keys stay honest.
assert.match(ts, /\[k: string\]: unknown \| undefined;/);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.