The Type System
Primitives, objects, unions, and the structural rule the whole language rests on.
The primitive types
One per JavaScript primitive, all lowercase:
const city: string = "London"
const age: number = 36
const admin: boolean = true
const id: symbol = Symbol("id")
const huge: bigint = 9007199254740993n
console.log(city, age, admin, id.toString(), huge)null and undefined are types too, and a string will not hold either:
let city: string = "London"
city = nullTo allow it, say so with a |:
let city: string | null = "London"
city = null
console.log(city)Arrays and tuples
string[] is an array of one type, however long:
const words: string[] = ["a", "b"]
const nums: Array<number> = [1, 2]
words.push(3)A tuple fixes both the length and the type at each position:
const point: [number, number] = [1, 2, 3]Positions can be named, which only serves readability:
type Point = [x: number, y: number]
const p: Point = [1, 2]
const [x, y] = p
console.log(x, y)Object types
Written inline, the shape goes straight in the annotation:
const user: { handle: string; age: number } = { handle: "ada", age: 36 }
console.log(user.handle)Name it once you need it twice:
interface User {
handle: string
}
const u: User = { handle: "ada" }
console.log(u.handel)interface vs type
Both describe an object shape, and here they are interchangeable:
interface Dog {
legs: number
}
type Cat = {
legs: number
}
const d: Dog = { legs: 4 }
const c: Cat = { legs: 4 }
console.log(d.legs, c.legs)type names any type, not just an object — an interface cannot do this:
type Id = string | number
type Pair = [number, number]
const a: Id = 7
const b: Pair = [1, 2]
console.log(a, b)What interface has instead is merging. Two declarations of one name combine:
interface Box {
width: number
}
interface Box {
height: number
}
const b: Box = { width: 1, height: 2 }
console.log(b.width, b.height)The same with type is a duplicate name:
type Box = { width: number }
type Box = { height: number }Structural typing
This is the rule the rest of the language rests on. A type matches when the fields match — the name it was declared under is irrelevant:
interface Named {
handle: string
}
function show(x: Named) {
return x.handle
}
const dog = { handle: "rex", legs: 4 }
console.log(show(dog))dog was never declared as a Named, has a field Named does not mention, and is
accepted. Extra fields are fine; missing ones are not.
A class needs no implements either:
interface Named {
handle: string
}
class Robot {
handle = "r2"
}
const r: Named = new Robot()
console.log(r.handle)Excess property checks
There is one exception to “extra fields are fine”. Assign a brand-new object literal and the extra field is an error:
interface Named {
handle: string
}
function show(x: Named) {
return x.handle
}
console.log(show({ handle: "rex", legs: 4 }))The same object through a variable passes — as it did above. Only a fresh literal is checked this strictly:
interface Options {
debug: boolean
}
function setup(o: Options) {
return o.debug
}
console.log(setup({ debug: true, verbos: true }))Optional and readonly
? makes a field skippable:
interface User {
handle: string
age?: number
}
const a: User = { handle: "ada" }
const b: User = { handle: "ada", age: 36 }
console.log(a.age, b.age)It also means the type is now number | undefined, so reading it is checked:
interface User {
handle: string
age?: number
}
const u: User = { handle: "ada" }
console.log(u.age.toFixed(0))readonly blocks assignment, and on an array it removes the mutating methods:
interface User {
readonly handle: string
tags: readonly string[]
}
const u: User = { handle: "ada", tags: ["admin"] }
u.handle = "linus"
u.tags.push("staff")It stops at one level down, though. This compiles clean:
interface User {
readonly profile: { city: string }
}
const u: User = { profile: { city: "London" } }
u.profile.city = "Paris"
console.log(u.profile.city)Unions
A | means one type or another:
let id: string | number = 7
id = "abc"
console.log(id)You only get what every member has. toUpperCase is not on number:
function label(id: string | number) {
return id.toUpperCase()
}toString is on both, so it is allowed:
function label(id: string | number) {
return id.toString()
}
console.log(label(7), label("abc"))Intersections
& means both at once, and is how you combine shapes:
type Timestamps = { createdAt: string }
type Post = { title: string }
const p: Post & Timestamps = { title: "hi", createdAt: "2026-01-01" }
console.log(p.title, p.createdAt)Both halves are required:
type Timestamps = { createdAt: string }
type Post = { title: string }
const p: Post & Timestamps = { title: "hi" }When a field conflicts you get no error here — the field’s type becomes the impossible
string & number, and nothing can be assigned to it:
type A = { id: string }
type B = { id: number }
type C = A & B
const c: C = { id: "x" }Literal types
A single value can be a type, and a union of them is the usual way to model a fixed set:
let role: "admin" | "guest" = "admin"
role = "owner"You get one from a const for free. A let widens to string, because it can be
reassigned:
const a = "admin"
let b = "admin"
const x: "admin" = a
const y: "admin" = bInside an object, every field widens. as const stops that, and makes the whole thing
readonly:
const config = { role: "admin" } as const
config.role = "guest"Which is what makes this array usable where the literal union is wanted:
const roles = ["admin", "guest"] as const
function go(r: "admin" | "guest") {
return r
}
console.log(roles.map(go))any
any switches the checker off for that value. Every one of these lines is accepted:
const v: any = "ada"
console.log(v.toFixed(2))
console.log(v.anything.at.all)Which only defers the problem to runtime:
const v: any = "ada"
try {
v.toFixed(2)
} catch (err) {
console.log((err as Error).message)
}Worse, it spreads. data.age is any, so assigning it to a number is not questioned:
const data: any = { age: "36" }
const age: number = data.age
console.log(age * 2)unknown
unknown is the honest version of the same idea: a value whose type you do not know. It
accepts anything in, and lets you do nothing with it:
const v: unknown = "ada"
console.log(v.toUpperCase())It will not quietly become a string either:
const v: unknown = "ada"
const s: string = vProve what it is, and it opens up:
const v: unknown = "ada"
if (typeof v === "string") {
console.log(v.toUpperCase())
}never and void
never is the type of a value that never arrives. A function that always throws returns
it:
function fail(msg: string): never {
throw new Error(msg)
}
console.log(typeof fail)Nothing is assignable to it, which is the property chapter 3 uses for exhaustiveness:
function fail(msg: string): never {
return msg
}void is different — the function does return, it just has no useful value:
function log(msg: string): void {
console.log(msg)
}
const r: string = log("hi")