b2KIT

TypeScript to JSON Schema

Convert TypeScript interfaces and types to JSON Schema definitions with property type mapping.

Tested tool guide Tested browser tools Checked August 16, 2026

What TypeScript to JSON Schema does, with a checked example

TypeScript interfaces and type aliases become JSON Schema documents here: each property is mapped to its schema type, arrays get an items keyword, unions of literals become an enum, and the required array is rebuilt from your ? optional markers. Two things usually surprise people. First, TypeScript has no required keyword - a converter's only signal for what belongs in required is the question mark, so a property written without one is assumed required even when real data often lacks it. Second, this tool converts any and unknown to an empty schema {} that accepts anything, and Date conventionally maps to a string with a date-time format. The conversion runs entirely in the browser; nothing you paste is uploaded.

Worked example

A concrete input and expected output from the current implementation.

Input

type User = {
  id: number;
  name?: string;
  tags: string[];
  role: 'admin' | 'member';
};

Expected output

{
  "type": "object",
  "properties": {
    "id": { "type": "number" },
    "name": { "type": "string" },
    "tags": { "type": "array", "items": { "type": "string" } },
    "role": { "enum": ["admin", "member"] }
  },
  "required": ["id", "tags", "role"]
}

name carries the ? marker, so it appears in properties but not in the required array, while the other three properties are listed there. tags becomes an array with an items schema, and the two-element string literal union collapses into an enum listing exactly 'admin' and 'member'.

How the result is produced

1

Parsing and structural mapping

The tool parses the TypeScript source you paste, picks out the interfaces, type aliases, and inline object literals it finds, then walks each declared shape. Scalar types map one-to-one - string becomes type: string, number becomes type: number, boolean becomes type: boolean - arrays gain an items keyword, nested object types become nested properties blocks, and the optional marker ? decides which properties are listed in the required array.

2

Edge cases in type mapping

Nullable unions such as string | null typically become a type array listing both, ['string', 'null']; a union of string literals becomes an enum; and Date follows the widespread convention of a string carrying format date-time. This tool maps types that JSON Schema cannot express - functions, promises, class instances, generic instantiations - to an empty schema {} that accepts any value, so a suspiciously permissive property usually points at an unmappable type rather than a deliberate choice.

Good uses

  • Generate a draft JSON Schema from your existing API request and response types so the validation schema and the TypeScript types cannot drift apart.
  • Produce a schema for a non-TypeScript consumer - a Python or Java service, an OpenAPI components section, or an API documentation page - without hand-writing verbose JSON Schema.
  • Create a starting schema for a validation library such as Ajv, then enrich it with formats, min/max bounds, and descriptions that the type system cannot express.

Limits and checks

  • The required array reflects only your ? markers. A property written without ? is required by the schema even when the runtime data frequently omits it, and JSON Schema offers no middle ground such as a default value to express 'usually present'.
  • any, unknown, and unmappable types such as functions or generics collapse to an empty schema {} that validates anything. A schema that accepts every payload is not proof of correctness - it is a sign a type could not be expressed.
  • Whether unknown extra keys are rejected depends on the additionalProperties keyword in the output. TypeScript's excess-property checks are compile-time only, and JSON Schema allows extra keys by default, so inspect the generated schema before trusting it to reject unexpected fields.

Common questions

I wrote name: string but the generated required array includes it even though the data sometimes lacks the field. What did I miss?

The question mark. Converters derive required membership solely from the ? marker, so name: string is required and name?: string is not. If a field is genuinely optional in the runtime data, mark it optional in the type. If it is always present but sometimes null, model it as string | null and validate the null case instead.

Will the schema reject objects that carry extra properties my interface never declared?

Only if the output sets additionalProperties: false. TypeScript never forbids extra keys at runtime - its excess-property checks happen at compile time - and JSON Schema permits unknown keys unless the schema explicitly forbids them. Check what the tool emitted for additionalProperties, and add the keyword yourself if strictness matters.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools