Course contentsShow
← TypeScript
Lesson 1464 of 1,79633. Inference Inversion PatternsFree lesson

Types as Values, Type Operators as Functions

Viewing type aliases and conditional types as functions that transform types.

Types as Values, Type Operators as Functions

You'll learn: How to think of TypeScript's type system as a functional programming language where types are data and type operators transform them.

The Mental Model Shift

In runtime JavaScript, you work with values and functions:

const value = 5;
const transform = (x: number) => x * 2;
const result = transform(value); // 10

TypeScript's type system works the exact same way, but at compile time:

type Value = number;
type Transform<T> = T[];
type Result = Transform<Value>; // number[]

Here, Value is like a variable holding the type number. Transform is like a function that takes a type and returns a new type. Just as transform(value) produces a new runtime value, Transform<Value> produces a new type.

Type Aliases Are Like Variables

When you write a type alias, you're essentially declaring a variable in the type-level language:

type UserId = string;        // const userId = "some-string"
type Count = number;         // const count = 42
type IsActive = boolean;     // const isActive = true

These "store" types just like variables store values.

Generic Types Are Functions

Generic type parameters let you write functions that transform types:

// Type-level function: wraps any type in an array
type WrapInArray<T> = T[];

type Numbers = WrapInArray<number>;  // number[]
type Strings = WrapInArray<string>;  // string[]

Think of <T> as a function parameter and T[] as the return value. You're defining logic at the type level.

Conditional Types Are If-Statements

Conditional types are the if/else of type-level programming:

// Type-level function with branching logic
type IsString<T> = T extends string ? true : false;

type A = IsString<"hello">;  // true
type B = IsString<number>;   // false

This reads: "If T extends string, return true type, otherwise return false type."

Key takeaway: TypeScript's type system is a functional programming language. Type aliases are values, generics are functions, and conditional types are control flow. Understanding this perspective unlocks advanced type manipulation patterns.