Skip to content
mohsami632-dotcomPublic

About

A developer-first, CLI and programmatic schema compiler for bidirectional, loss-aware conversion between schema definition formats.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

typeshift

CI npm version License: MIT TypeScript Node.js

typeshift is a developer-first schema compiler for bidirectional, loss-aware conversion between schema definition formats.

               ┌───────────────────────┐
               │    TypeScript (.ts)   │
               └───────────▲───────────┘
                           │
       ┌───────────────────┼───────────────────┐
       │                   ▼                   │
┌──────┴──────┐      ┌───────────┐      ┌──────┴──────┐
│ JSON Schema │ <──> │  SchemaIR │ <──> │ Zod (.zod)  │
└──────┬──────┘      └───────────┘      └──────┬──────┘
       │                   ▲                   │
       └───────────────────┼───────────────────┘
                           │
               ┌───────────▼───────────┐
               │   OpenAPI 3.1 (.json) │
               └───────────────────────┘

Why typeshift?

Modern systems inevitably maintain schemas across multiple boundaries:

  • TypeScript interfaces for application logic and compile-time types.
  • JSON Schema for event streams, storage validation, and API contracts.
  • Zod schemas for runtime request/response parsing and form validation.
  • OpenAPI 3.1 specifications for public REST APIs and client generation.

When schemas drift across layers, bugs slip into production. Translating schemas across diverse format boundaries introduces several core challenges:

  1. Mismatched Format Expressiveness: Different schema formats have fundamentally different capabilities. For example, a JSON Schema with numeric bounds or regex patterns cannot fully express those validation constraints in standard compile-time TypeScript interfaces. Without explicit visibility, developers may assume target schemas carry the same guarantees as source schemas.
  2. Combinatorial Complexity ($N \times (N-1)$): Supporting 4 formats with point-to-point tools requires managing 12 separate pairs; supporting 6 requires 30.
  3. CI Parity Verification: Multi-tool workflows often lack a uniform mechanism to enforce schema fidelity and detect unrepresentable changes across layers.

typeshift solves this with a compiler model:

  • Canonical Intermediate Representation (SchemaIR): All formats parse into and generate from a unified AST ($2N$ scaling). Adding a format requires only 2 functions (parse and generate).
  • Explicit Information-Loss Diagnostics: Every conversion inspects source constraints against target format capabilities. If information cannot be expressed natively, typeshift reports exact paths and dropped constraints.
  • CI Enforcement: --loss-policy error immediately fails automated pipelines (exit code 2) if unauthorized loss occurs.
  • Deterministic Generation: Alphabetically sorted keys and normalized layout guarantee character-identical output and zero git diff churn.
  • Robust Parser Architecture: Direct integration with the official TypeScript Compiler API for AST parsing, paired with Commander for CLI execution and a lightweight core transformation engine.

Quick Start: Try It in 30 Seconds

Option A: Instant Shell Pipe (Zero Files Required)

Convert a TypeScript interface directly to JSON Schema without creating any files:

# On Linux, macOS, or PowerShell:
echo 'export interface User { id: string; name: string; age?: number; }' | npx @mohsami/typeshift convert --from ts --to json-schema

Option B: Local File Conversion

1. Create a minimal sample schema (user.ts):

// user.ts
export interface User {
  id: string;
  name: string;
  age?: number;
  role: 'admin' | 'member';
}

2. Compile to a Draft-07 JSON Schema:

npx @mohsami/typeshift convert user.ts --to json-schema -o user.schema.json

3. Compile to a runtime Zod validator:

npx @mohsami/typeshift convert user.ts --to zod -o user.zod.ts

4. Explore complete committed examples:

Explore the examples/ directory for tested sample files covering TypeScript, JSON Schema, Zod, OpenAPI 3.1, and CI quality gates.


Installation

Global CLI

# Using pnpm
pnpm add -g @mohsami/typeshift

# Using npm
npm install -g @mohsami/typeshift

# Using yarn
yarn global add @mohsami/typeshift

Local Project Dependency

pnpm add @mohsami/typeshift
# or
npm install @mohsami/typeshift

Visual Example: Loss Detection & CI Enforcement

When converting rich schemas into formats with weaker constraint semantics (e.g., JSON Schema to compile-time TypeScript), typeshift alerts you to dropped invariants:

$ typeshift convert schemas/user.json --to typescript
Information-Loss Diagnostics (3 warnings):
  ▲ WARNING [User.email] Constraint "format" (value: "email") will be dropped. (format constraint)
  ▲ WARNING [User.age] Constraint "minimum" (value: 0) will be dropped. (minimum constraint)
  ▲ WARNING [User.age] Constraint "maximum" (value: 120) will be dropped. (maximum constraint)

// Generated by typeshift — do not edit manually.

export interface User {
  email: string;
  age?: number;
}

Enforcing Strict Loss Policy in CI

To ensure schema parity across teams and prevent accidental degradation:

# Aborts conversion and exits with code 2 if any constraint is dropped
typeshift convert schemas/user.json --to typescript --loss-policy error
Information-Loss Diagnostics (3 warnings):
  ▲ WARNING [User.email] Constraint "format" (value: "email") will be dropped.
  ▲ WARNING [User.age] Constraint "minimum" (value: 0) will be dropped.
  ▲ WARNING [User.age] Constraint "maximum" (value: 120) will be dropped.
✖ Aborting due to --loss-policy error. Information loss was detected during conversion.

CI Workflow Example

Add schema verification to your GitHub Actions pipeline:

name: Verify Schema Parity
on: [push, pull_request]

jobs:
  verify:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: 9
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: pnpm

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Verify zero schema drift
        run: |
          npx @mohsami/typeshift convert contracts/api.openapi.json --to json-schema --loss-policy error -o dist/api.schema.json
          git diff --exit-code dist/api.schema.json

CLI Usage Guide

1. File Conversion

# TypeScript -> JSON Schema
typeshift convert src/models.ts --to json-schema -o schemas/models.json

# OpenAPI 3.1 -> TypeScript
typeshift convert openapi.json --to typescript -o src/api-types.ts

# OpenAPI 3.1 -> Zod
typeshift convert openapi.json --to zod -o src/validators.zod.ts

# JSON Schema -> OpenAPI 3.1
typeshift convert schema.json --to openapi -o spec.openapi.json

2. Format Aliases & Auto-Detection

Source format is automatically inferred from file extension (.ts, .json, .openapi.json, .zod.ts), or can be specified with short aliases:

Format Format ID Aliases File Extensions
TypeScript typescript ts .ts
JSON Schema json-schema jsonschema, json .json, .schema.json
Zod zod z .zod.ts
OpenAPI 3.1 openapi oas, oas3, openapi3.1 .openapi.json, .oas.json, openapi.json
typeshift convert schema.ts --to z
typeshift convert openapi.json --to ts

3. Shell Piping (stdin / stdout)

cat types.ts | typeshift convert --from ts --to json-schema

4. Inspect Available Formats & Capabilities

typeshift list --detailed
Available Schema Formats:

  typescript       TypeScript       Extensions: .ts (Aliases: ts)
    Capabilities: -constraints +docs -defaults +nullable +unions +intersections +recursion

  json-schema      JSON Schema      Extensions: .json, .schema.json (Aliases: jsonschema, json)
    Capabilities: +constraints +docs +defaults +nullable +unions +intersections +recursion

  zod              Zod              Extensions: .zod.ts (Aliases: z)
    Capabilities: +constraints +docs +defaults +nullable +unions +intersections -recursion

  openapi          OpenAPI 3.1      Extensions: .openapi.json, .oas.json, openapi.json, .openapi (Aliases: oas, oas3, openapi3, openapi-3.1, openapi3.1)
    Capabilities: +constraints +docs +defaults +nullable +unions +intersections +recursion

5. Validate Schemas

# Human-readable summary
typeshift validate openapi.json

# Structured JSON output for scripts
typeshift validate openapi.json --json

Supported Formats Matrix

Feature TypeScript (ts) JSON Schema (json-schema) Zod (zod) OpenAPI 3.1 (openapi)
Bidirectional ✅ ✅ ✅ ✅
Object / Interfaces ✅ ✅ ✅ ✅
Numeric Constraints (min, max, multipleOf) ❌ (warned) ✅ ✅ ✅
String Constraints (pattern, minLength, maxLength) ❌ (warned) ✅ ✅ ✅
Format Hints (email, uuid, uri, date-time) ❌ (warned) ✅ ✅ ✅
Default Values ❌ (warned) ✅ ✅ ✅
Unions & Discriminated Unions ✅ ✅ ✅ ✅
Intersections ✅ ✅ ✅ ✅
Enums & Literals ✅ ✅ ✅ ✅
Tuples & Records ✅ ✅ ✅ ✅
Nullable & Optional Modifiers ✅ ✅ ✅ ✅
Documentation / JSDoc / Descriptions ✅ ✅ ✅ ✅
Component References ($ref) ✅ ✅ ✅ ✅
ReadOnly Property Attributes ✅ ❌ (warned) ❌ (warned) ✅

Important

OpenAPI 3.1 Scope & Boundaries:

  • In Scope: Data models declared under components.schemas (object definitions, scalar constraints, enums, unions, intersections, references via #/components/schemas/<Name>, and readOnly attributes).
  • Not in Scope: HTTP API routing operations under paths (endpoints, HTTP methods, headers, route parameters). typeshift is a schema model compiler, not a full client/server code generator.
  • File Formats: OpenAPI specifications must be provided in JSON format (.openapi.json, .oas.json, openapi.json, or .json). Direct YAML (.yaml/.yml) parsing is not yet supported natively; providing a YAML file gives clear, actionable instructions. Tracking issue: #5.

Programmatic API

typeshift is fully typed and exports a TypeScript programmatic API.

import { convert, parse, generate, S } from '@mohsami/typeshift';

// High-level bidirectional conversion
const result = convert(
  `
  export interface User {
    id: string;
    username: string;
    email: string;
    role?: 'admin' | 'member';
  }
  `,
  {
    from: 'typescript',
    to: 'openapi',
  },
);

console.log(result.output);
// Emits valid OpenAPI 3.1.0 document with components.schemas.User

console.log(result.diagnostics);
// Emits diagnostics array if information loss was detected

Building Schemas Manually (Fluent SchemaIR)

import { S, generate } from '@mohsami/typeshift';

const doc = S.document({
  Product: S.object({
    sku: S.required(S.string({ minLength: 3 })),
    price: S.required(S.number({ minimum: 0 })),
    tags: S.optional(S.array(S.string())),
  }),
});

// Generate Zod code from IR
const zodCode = generate(doc, 'zod');
console.log(zodCode);

// Generate OpenAPI 3.1 from IR
const openApiDoc = generate(doc, 'openapi');
console.log(openApiDoc);

For complete API details, refer to the API Reference.


Architecture

                  ┌─────────────────┐
                  │ Source Document │
                  └────────┬────────┘
                           │
                     parse() (Adapter)
                           │
                           ▼
                  ┌─────────────────┐
                  │    Schema IR    │
                  └────────┬────────┘
                           │
            ┌──────────────┴──────────────┐
            ▼                             ▼
   ┌─────────────────┐           ┌──────────────────┐
   │  Loss Detector  │           │    generate()    │
   │  (Diagnostics)  │           │    (Adapter)     │
   └────────┬────────┘           └────────┬─────────┘
            │                             │
            ▼                             ▼
       Diagnostics                  Target Output

Read our comprehensive documentation:


Contributing

We welcome community contributions! Because each format adapter is an isolated module, adding support for new formats (such as GraphQL, SQL DDL, or Protobuf) is clean, approachable, and self-contained.


Roadmap

  • v0.1.0 — Core SchemaIR, TypeScript, JSON Schema, and Zod adapters, loss detection, CLI.
  • v0.2.0 — OpenAPI 3.1 component schemas adapter, comprehensive test matrix, format aliases, loss-policy exit codes.
  • v0.3.0 — GraphQL SDL adapter (#1), OpenAPI YAML input (#5), file watch mode (#3).
  • v0.4.0 — SQL DDL adapter (#2), expanded JSON Schema keywords (#4), configuration file support (typeshift.config.ts).
  • v1.0.0 — Protobuf message adapter, external plugin loading from npm, performance benchmarks for multi-megabyte schemas.

License

MIT © 2026 typeshift Contributors.

About

A developer-first, CLI and programmatic schema compiler for bidirectional, loss-aware conversion between schema definition formats.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages