runtime-type-reconstruction

Use when validating data at runtime. Use when parsing JSON. Use when types need runtime checks. Use when using io-ts or zod. Use when building validation schemas.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "runtime-type-reconstruction" with this command: npx skills add marius-townhouse/effective-typescript-skills/marius-townhouse-effective-typescript-skills-runtime-type-reconstruction

Know How to Reconstruct Types at Runtime

Overview

TypeScript types are erased at runtime, but sometimes you need to validate that runtime data matches your types. Use libraries like io-ts, zod, or runtypes to define schemas that provide both runtime validation and static types. This ensures your types match reality.

When to Use This Skill

  • Validating data at runtime
  • Parsing JSON with type safety
  • Types need runtime checks
  • Using io-ts, zod, or runtypes
  • Building validation schemas

The Iron Rule

Use validation libraries like zod or io-ts to get both runtime validation and TypeScript types from a single source of truth.

Example with Zod

import { z } from 'zod';

// Define schema once
const UserSchema = z.object({
  id: z.string(),
  name: z.string(),
  email: z.string().email(),
  age: z.number().optional(),
});

// Get TypeScript type from schema
type User = z.infer<typeof UserSchema>;

// Runtime validation
const result = UserSchema.safeParse(unknownData);
if (result.success) {
  // result.data is typed as User
  console.log(result.data.name);
} else {
  console.error(result.error);
}

Reference

  • Effective TypeScript, 2nd Edition by Dan Vanderkam
  • Item 74: Know How to Reconstruct Types at Runtime

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

tsdoc-comments

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

code-gen-independent

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

module-by-module-migration

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

tsconfig-options

No summary provided by upstream source.

Repository SourceNeeds Review