Skip to content

Type-Level Programming

In one line

Type-level programming computes types using conditional types, infer, and recursion — no runtime code needed.

In simple words

Type-level programming means using TypeScript's type system as a programming language — performing computations entirely at the type level, with no runtime code.

The type system is Turing-complete (with recursion limits), so you can:

  1. Arithmetic — add, subtract, multiply type-level numbers
  2. String manipulation — uppercase, replace, trim at the type level
  3. Logic — And, Or, Not on type-level booleans
  4. Data structures — type-level lists, maps, trees
  5. Pattern matching — extract and transform types

Tools: conditional types (T extends U ? X : Y), infer, mapped types, template literal types. Useful for generating types from schemas, validation, and metaprogramming.

Example

typescript
// Type-level string operations
type Upper<S extends string> = Uppercase<S>;
type Greeting = Upper<"hello">; // "HELLO"

// Type-level addition (simplified)
type Add<A extends number, B extends number> = 
  [...BuildTuple<A>, ...BuildTuple<B>]["length"];
type BuildTuple<N extends number, T extends unknown[] = []> = 
  T["length"] extends N ? T : BuildTuple<N, [...T, unknown]>;
type Sum = Add<2, 3>; // 5

// Template literal type manipulation
type Getters<T> = {
  [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
type PersonGetters = Getters<{ name: string; age: number }>;
// { getName: () => string; getAge: () => number }

These types compute at compile time: Upper<'hello'> becomes 'HELLO', Add<2,3> becomes 5, Getters generates getter method types. All with zero runtime code.

Common confusions

  • Confused with: generic types

    The difference: Generic types parameterize over other types for reuse (List<T>). Type-level programming uses the type system to compute and transform types — it's computation, not just parameterization. Generics are variables; type-level programming is functions on types.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary