Skip to content
webtype.orgTwelve concepts · 1/12

Concept 1 of 12

Type aliases

A type alias gives a name to a type you could have written inline. That is all it does — it creates no new type, only a shorter way to refer to an existing one. Every puzzle on this site is ultimately one type alias, which is why this is where the climb starts.

An alias is transparent

type ID = string

// These two signatures are the same type, not merely compatible ones.
declare function save(id: ID): void
declare function save(id: string): void

The compiler substitutes the alias wherever it appears. `ID` and `string` are interchangeable in every position, and an error message may print either one depending on which is shorter to display.

This is what separates an alias from a class or an enum, which do create new types. If you want two string-shaped things the compiler refuses to mix up, an alias will not give you that — you need a branded type.

Aliases can name anything

type Point = { x: number; y: number }
type Direction = 'up' | 'down'
type Handler = (event: string) => void
type Pair = [number, number]

Unions, functions, tuples and object shapes can all be named. An `interface` can only describe an object, which is the practical reason most type-level code reaches for `type` instead.

The common wrong answer

type Meters = number
type Feet = number

declare function jump(height: Meters): void
jump(12 as Feet) // no error — both are just number

Aliases do not create distinct types, so this compiles happily and the units silently disagree. Naming a type documents intent for readers; it does not enforce anything for the compiler.

Takeaway

An alias is a nickname, not a new thing. Everything that follows in this track is the same nickname with parameters attached.