home / skills / epicenterhq / epicenter / typebox

typebox skill

/.agents/skills/typebox

This skill helps you validate runtime types and switch between TypeBox and TypeMap for fast, standards-compatible schema validation.

npx playbooks add skill epicenterhq/epicenter --skill typebox

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.2 KB
---
name: typebox
description: TypeBox and TypeMap patterns for schema validation. Use when working with runtime type validation, JSON Schema, or Standard Schema.
---

# TypeBox and TypeMap

## Package Names

**Use `typebox`, not `@sinclair/typebox`**. The `@sinclair/typebox` package is deprecated.

```typescript
// Correct
import { Type } from 'typebox';
import { Compile } from 'typebox/compile';
import { Value } from 'typebox/value';

// Wrong - deprecated
import { Type } from '@sinclair/typebox';
```

## When to Use What

| Need                          | Use                        |
| ----------------------------- | -------------------------- |
| Define schemas                | `typebox` with `Type.*`    |
| Standard Schema support       | `@sinclair/typemap`        |
| Translate between libraries   | `@sinclair/typemap`        |
| High-performance validation   | `Compile()` from either    |
| One-off validation            | `Value.Check()` from typebox |

## TypeMap for Standard Schema

TypeBox doesn't implement Standard Schema natively. Use TypeMap:

```typescript
import { Compile } from '@sinclair/typemap';
import { Type } from 'typebox';

// From TypeBox schema
const validator = Compile(
	Type.Object({
		name: Type.String(),
		age: Type.Number(),
	}),
);

// Standard Schema interface
const result = validator['~standard'].validate({ name: 'Alice', age: 30 });
```

## TypeMap Accepts Everything

`Compile()` from TypeMap accepts:

```typescript
import { Compile } from '@sinclair/typemap';

// TypeScript syntax strings
const v1 = Compile(`{ name: string, age: number }`);

// TypeBox schemas
const v2 = Compile(Type.Object({ x: Type.Number() }));

// Zod schemas
const v3 = Compile(z.object({ x: z.number() }));

// Valibot schemas
const v4 = Compile(v.object({ x: v.number() }));
```

All return validators with `['~standard'].validate()`.

## TypeBox Compile vs TypeMap Compile

```typescript
// TypeBox Compile - returns Validator with Check/Parse
import { Compile } from 'typebox/compile';
const validator = Compile(schema);
validator.Check(value); // boolean
validator.Parse(value); // throws or returns typed value

// TypeMap Compile - returns Standard Schema validator
import { Compile } from '@sinclair/typemap';
const validator = Compile(schema);
validator['~standard'].validate(value); // { value } or { issues }
```

Use TypeMap when you need Standard Schema compatibility. Use TypeBox directly when you don't.

## Translation Functions

TypeMap translates between libraries:

```typescript
import { Syntax, TypeBox, Zod, Valibot } from '@sinclair/typemap';

const syntax = `{ name: string }`;
const tbSchema = TypeBox(syntax);
const zodSchema = Zod(syntax);
const valibotSchema = Valibot(syntax);
const backToSyntax = Syntax(zodSchema);
```

## Performance

TypeMap's compiled validators are ~100x faster than native Zod:

| Library        | 10M iterations |
| -------------- | -------------- |
| Zod native     | ~4,669ms       |
| TypeMap        | ~47ms          |

## References

- [TypeMap is the Real Deal](../../docs/articles/typemap-is-the-real-deal.md)
- [TypeBox is a Beast](../../docs/articles/typebox-is-a-beast.md)
- [Why TypeBox Won't Implement Standard Schema](../../docs/articles/typebox-standard-schema-criticisms.md)

Overview

This skill documents TypeBox and TypeMap patterns for runtime schema validation and Standard Schema interoperability. It clarifies which packages to import, when to use TypeBox versus TypeMap, and how to compile high-performance validators. The guidance focuses on practical imports, translation between schema libraries, and performance trade-offs.

How this skill works

TypeBox provides schema builders (Type.*) and a Compile utility that returns fast validators with Check and Parse methods for one-off or compiled validation. TypeMap (typemap) accepts many schema inputs — TypeBox schemas, TypeScript syntax strings, Zod, Valibot — and produces Standard Schema-compatible validators accessible via validator['~standard'].validate(). Use TypeBox directly for native workflows and TypeMap when you need Standard Schema compatibility or library translation.

When to use it

  • Use typebox (import from 'typebox') to define schemas with Type.* and perform native Check/Parse validation.
  • Use @sinclair/typemap (TypeMap) when you need Standard Schema support or to translate between different schema libraries.
  • Use TypeBox Compile for high-performance compiled validators with Check/Parse.
  • Use TypeMap Compile when you require a validator that exposes the Standard Schema interface (~standard).
  • Use Value.Check for simple, one-off checks without compilation.

Best practices

  • Import from 'typebox' — the @sinclair/typebox package is deprecated.
  • Prefer Compile from typebox/compile for repeated, high-performance validation in hot paths.
  • Use typemap's Compile when integrating with tools or services that expect Standard Schema behavior.
  • Translate schemas via TypeMap when migrating or bridging Zod, Valibot, or TypeScript syntax to a unified format.
  • Benchmark critical paths: TypeMap compiled validators are often far faster than native Zod for large iteration counts.

Example use cases

  • Define an API request/response schema with TypeBox and compile a validator for runtime safety.
  • Translate a Zod schema to a Standard Schema using TypeMap for cross-library interoperability.
  • Compile a TypeBox schema into a Standard Schema validator to satisfy an external validation contract.
  • Run millions of validations in tight loops using compiled validators for best performance.
  • Perform quick, one-off validation checks with Value.Check when compilation overhead is unnecessary.

FAQ

Which package should I import for new projects?

Import from 'typebox' for TypeBox functionality; avoid the deprecated '@sinclair/typebox'. Use '@sinclair/typemap' when you need Standard Schema support or translation features.

When do I use Compile vs Value.Check?

Use Compile for repeatedly executed, high-performance validation (offers Check/Parse). Use Value.Check for occasional, one-off boolean checks without compilation.