0Pricing
TypeScript Academy · Lesson

Types as a Computation Language

Understand the type system as a pure functional language.

Types as a Computation Language is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Types Are a Language

TypeScript has two languages living side by side. One is the value-level JavaScript you already know. The other is the type level, which runs entirely at compile time. In this course you learn to program in that second language.

The type system is a small, pure, functional language. You give it types as input and it computes types as output. None of it survives to runtime.

type Greeting = "hello";
type Loud = Uppercase<Greeting>;
// Loud is "HELLO" - computed by the type system

Types In, Types Out

A generic type is essentially a function from types to types. The type parameter is the argument, and the body is the return value.

Below, Boxed takes a type T and produces an object type that wraps it. Think of T as a parameter you pass in.

type Boxed<T> = { value: T };

type A = Boxed<number>; // { value: number }
type B = Boxed<string>; // { value: string }

Generics Are Functions

Compare a value-level function with a type-level one. The shapes are nearly identical: parameters in, a single result out.

  • Value: const id = (x) => x
  • Type: type Id<T> = T

The type-level Id simply returns whatever you give it.

type Id<T> = T;

type X = Id<boolean>; // boolean
type Y = Id<"abc">;  // "abc"

No Runtime Cost

Everything at the type level is erased before your code runs. The JavaScript output contains zero traces of your type computations. There is no performance cost in the running program, no matter how elaborate your types are.

This is why type-level programming is sometimes called free: it only affects the compiler, never the bundle.

type Pair<T> = [T, T];
const p: Pair<number> = [1, 2];
// Compiled JS is just: const p = [1, 2];
console.log(p);

Multiple Parameters

Type-level functions can take several parameters, just like ordinary functions. Here Merge takes two object types and combines them.

Read it as: given A and B, return an object that has all properties of both.

type Merge<A, B> = A & B;

type User = { id: number };
type Named = { name: string };
type NamedUser = Merge<User, Named>;
// { id: number; name: string }

Defaults for Parameters

Type parameters can have default values, exactly like default function arguments. If the caller omits the argument, the default is used.

type List<T = string> = T[];

type A = List;        // string[]
type B = List<number>; // number[]

Constraints Are Guards

A constraint with extends restricts which types may be passed in. It is the type-level equivalent of validating a function argument before using it.

HasId only accepts types that already have an id property, so the body can safely read it.

type GetId<T extends { id: number }> = T["id"];

type A = GetId<{ id: number; name: string }>; // number
// GetId<{ name: string }> would be a compile error

Computation, Not Just Annotation

Beginners use types only to label values. Type-level programming uses types to compute answers. The built-in ReturnType utility, for example, inspects a function type and extracts its result type.

type Fn = (a: number) => string;
type R = ReturnType<Fn>; // string

type Fn2 = () => boolean;
type R2 = ReturnType<Fn2>; // boolean

Mapping Over Properties

Mapped types let you transform every property of an object type. This is a loop in the type language. Stringify turns every value type into string.

type Stringify<T> = { [K in keyof T]: string };

type Input = { a: number; b: boolean };
type Out = Stringify<Input>; // { a: string; b: string }

Pure and Deterministic

The type language is pure: the same inputs always produce the same output type, with no side effects. There is no mutation, no IO, no time. This purity is what makes type-level programs predictable and composable.

Because of purity, you reason about types the way you reason about math: by substitution.

type Square<T extends { area: number }> = T["area"];
// Always the same result for the same input shape

Why It Matters

Mastering this second language lets you encode rules the compiler enforces for free: valid routes, exhaustive switches, safe string manipulation, and APIs that cannot be misused. The rest of this course builds the toolbox.

Key mindset: a type is a value in the type language, and a generic is a function.

type NonEmpty<T extends unknown[]> =
  T extends [unknown, ...unknown[]] ? T : never;
// Encodes a rule: the tuple must have at least one element

Quick Check

Test your understanding of types as a computation language.

Recap

You learned that TypeScript types form a pure functional language that runs at compile time.

  • Generics are functions: types in, types out.
  • Parameters support defaults and extends constraints.
  • Mapped types loop over properties.
  • Everything is erased, so there is no runtime cost.

Next, you give this language an if statement: conditional types.

Frequently asked questions

Is the “Types as a Computation Language” lesson free?

Yes — the full text of “Types as a Computation Language” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Types as a Computation Language”?

Understand the type system as a pure functional language. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Types as a Computation Language” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Types as a Computation Language
  2. Type-Level Conditionals
  3. Type-Level Recursion
  4. Distributive Conditional Types
← Back to TypeScript Academy