
Stop Reaching for any: Narrowing unknown in TypeScript
Why unknown is the type-safe replacement for any, and the narrowing patterns that make it practical in real code.
Tags
12 articles

Why unknown is the type-safe replacement for any, and the narrowing patterns that make it practical in real code.

Use TypeScript template literal types to encode event names, route patterns and permission sets in the type system — no runtime checks, no string drift.

The satisfies operator validates that a value matches a type while preserving literal inference — why that distinction matters and where to apply it.

Replace Node's stringly-typed EventEmitter with a generic typed alternative that catches event name typos and payload mismatches at compile time.

Eliminate a whole class of runtime bugs by teaching TypeScript the difference between a UserId and an OrderId — even when both are strings underneath.

TypeScript's type system stops at compile time — Zod closes the gap by validating data at the runtime boundaries where it actually matters.

Move beyond try-catch soup: discriminated unions, Result types, and error boundaries that make TypeScript codebases more reliable and maintainable.

Advanced TypeScript patterns I use daily: discriminated unions, branded types, the builder pattern and utility types that keep large codebases safe.

A practical guide to end-to-end type safety in GraphQL with schema-first code generation: typed resolvers, client query types and no runtime mismatches.

A complete tutorial on end-to-end type-safe APIs with tRPC and the Next.js App Router: router setup, middleware, input validation and subscriptions.

Build a type-safe form library in TypeScript with runtime validation, field-level error tracking, dirty state and a composable API that catches errors early.

Build a code generator that reads OpenAPI specs and emits fully typed TypeScript API clients with request validation, error handling and type inference.