Skip to content
Sign inGet all access
Chapter 2·32 minFree

The Type System

Primitives, objects, unions, and the structural rule the whole language rests on.

The primitive types

One per JavaScript primitive, all lowercase:

app.ts
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)
Output$ npx tsc --noEmit app.ts$ echo $?0

null and undefined are types too, and a string will not hold either:

app.ts
let city: string = "London"

city = null
Output$ npx tsc --noEmit app.tsapp.ts(3,1): error TS2322: Type 'null' is not assignable to type 'string'.

To allow it, say so with a |:

app.ts
let city: string | null = "London"

city = null
console.log(city)
Output$ npx tsc --noEmit app.ts$ echo $?0

Arrays and tuples

string[] is an array of one type, however long:

app.ts
const words: string[] = ["a", "b"]
const nums: Array<number> = [1, 2]

words.push(3)
Output$ npx tsc --noEmit app.tsapp.ts(4,12): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.

A tuple fixes both the length and the type at each position:

app.ts
const point: [number, number] = [1, 2, 3]
Output$ npx tsc --noEmit app.tsapp.ts(1,7): error TS2322: Type '[number, number, number]' is not assignable to type '[number, number]'. Source has 3 element(s) but target allows only 2.

Positions can be named, which only serves readability:

app.ts
type Point = [x: number, y: number]

const p: Point = [1, 2]

const [x, y] = p
console.log(x, y)
Output$ npx tsc --noEmit app.ts$ echo $?0

Object types

Written inline, the shape goes straight in the annotation:

app.ts
const user: { handle: string; age: number } = { handle: "ada", age: 36 }

console.log(user.handle)
Output$ npx tsc --noEmit app.ts$ echo $?0

Name it once you need it twice:

app.ts
interface User {
  handle: string
}

const u: User = { handle: "ada" }

console.log(u.handel)
Output$ npx tsc --noEmit app.tsapp.ts(7,15): error TS2551: Property 'handel' does not exist on type 'User'. Did you mean 'handle'?

interface vs type

Both describe an object shape, and here they are interchangeable:

app.ts
interface Dog {
  legs: number
}

type Cat = {
  legs: number
}

const d: Dog = { legs: 4 }
const c: Cat = { legs: 4 }

console.log(d.legs, c.legs)
Output$ npx tsc --noEmit app.ts$ echo $?0

type names any type, not just an object — an interface cannot do this:

app.ts
type Id = string | number
type Pair = [number, number]

const a: Id = 7
const b: Pair = [1, 2]

console.log(a, b)
Output$ npx tsc --noEmit app.ts$ echo $?0

What interface has instead is merging. Two declarations of one name combine:

app.ts
interface Box {
  width: number
}

interface Box {
  height: number
}

const b: Box = { width: 1, height: 2 }

console.log(b.width, b.height)
Output$ npx tsc --noEmit app.ts$ echo $?0

The same with type is a duplicate name:

app.ts
type Box = { width: number }
type Box = { height: number }
Output$ npx tsc --noEmit app.tsapp.ts(1,6): error TS2300: Duplicate identifier 'Box'.app.ts(2,6): error TS2300: Duplicate identifier 'Box'.

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:

app.ts
interface Named {
  handle: string
}

function show(x: Named) {
  return x.handle
}

const dog = { handle: "rex", legs: 4 }

console.log(show(dog))
Output$ npx tsc --noEmit app.ts$ echo $?0

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:

app.ts
interface Named {
  handle: string
}

class Robot {
  handle = "r2"
}

const r: Named = new Robot()

console.log(r.handle)
Output$ npx tsc --noEmit app.ts$ echo $?0

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:

app.ts
interface Named {
  handle: string
}

function show(x: Named) {
  return x.handle
}

console.log(show({ handle: "rex", legs: 4 }))
Output$ npx tsc --noEmit app.tsapp.ts(9,35): error TS2353: Object literal may only specify known properties, and 'legs' does not exist in type 'Named'.

The same object through a variable passes — as it did above. Only a fresh literal is checked this strictly:

app.ts
interface Options {
  debug: boolean
}

function setup(o: Options) {
  return o.debug
}

console.log(setup({ debug: true, verbos: true }))
Output$ npx tsc --noEmit app.tsapp.ts(9,34): error TS2353: Object literal may only specify known properties, and 'verbos' does not exist in type 'Options'.

Optional and readonly

? makes a field skippable:

app.ts
interface User {
  handle: string
  age?: number
}

const a: User = { handle: "ada" }
const b: User = { handle: "ada", age: 36 }

console.log(a.age, b.age)
Output$ npx tsc --noEmit app.ts$ echo $?0

It also means the type is now number | undefined, so reading it is checked:

app.ts
interface User {
  handle: string
  age?: number
}

const u: User = { handle: "ada" }

console.log(u.age.toFixed(0))
Output$ npx tsc --noEmit app.tsapp.ts(8,13): error TS18048: 'u.age' is possibly 'undefined'.

readonly blocks assignment, and on an array it removes the mutating methods:

app.ts
interface User {
  readonly handle: string
  tags: readonly string[]
}

const u: User = { handle: "ada", tags: ["admin"] }

u.handle = "linus"
u.tags.push("staff")
Output$ npx tsc --noEmit app.tsapp.ts(8,3): error TS2540: Cannot assign to 'handle' because it is a read-only property.app.ts(9,8): error TS2339: Property 'push' does not exist on type 'readonly string[]'.

It stops at one level down, though. This compiles clean:

app.ts
interface User {
  readonly profile: { city: string }
}

const u: User = { profile: { city: "London" } }

u.profile.city = "Paris"
console.log(u.profile.city)
Output$ npx tsc --noEmit app.ts$ echo $?0

Unions

A | means one type or another:

app.ts
let id: string | number = 7

id = "abc"
console.log(id)
Output$ npx tsc --noEmit app.ts$ echo $?0

You only get what every member has. toUpperCase is not on number:

app.ts
function label(id: string | number) {
  return id.toUpperCase()
}
Output$ npx tsc --noEmit app.tsapp.ts(2,13): error TS2339: Property 'toUpperCase' does not exist on type 'string | number'. Property 'toUpperCase' does not exist on type 'number'.

toString is on both, so it is allowed:

app.ts
function label(id: string | number) {
  return id.toString()
}

console.log(label(7), label("abc"))
Output$ npx tsc --noEmit app.ts$ echo $?0

Intersections

& means both at once, and is how you combine shapes:

app.ts
type Timestamps = { createdAt: string }
type Post = { title: string }

const p: Post & Timestamps = { title: "hi", createdAt: "2026-01-01" }

console.log(p.title, p.createdAt)
Output$ npx tsc --noEmit app.ts$ echo $?0

Both halves are required:

app.ts
type Timestamps = { createdAt: string }
type Post = { title: string }

const p: Post & Timestamps = { title: "hi" }
Output$ npx tsc --noEmit app.tsapp.ts(4,7): error TS2322: Type '{ title: string; }' is not assignable to type 'Post & Timestamps'. Property 'createdAt' is missing in type '{ title: string; }' but required in type 'Timestamps'.

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:

app.ts
type A = { id: string }
type B = { id: number }

type C = A & B

const c: C = { id: "x" }
Output$ npx tsc --noEmit app.tsapp.ts(6,16): error TS2322: Type 'string' is not assignable to type 'never'.

Literal types

A single value can be a type, and a union of them is the usual way to model a fixed set:

app.ts
let role: "admin" | "guest" = "admin"

role = "owner"
Output$ npx tsc --noEmit app.tsapp.ts(3,1): error TS2322: Type '"owner"' is not assignable to type '"admin" | "guest"'.

You get one from a const for free. A let widens to string, because it can be reassigned:

app.ts
const a = "admin"
let b = "admin"

const x: "admin" = a
const y: "admin" = b
Output$ npx tsc --noEmit app.tsapp.ts(5,7): error TS2322: Type 'string' is not assignable to type '"admin"'.

Inside an object, every field widens. as const stops that, and makes the whole thing readonly:

app.ts
const config = { role: "admin" } as const

config.role = "guest"
Output$ npx tsc --noEmit app.tsapp.ts(3,8): error TS2540: Cannot assign to 'role' because it is a read-only property.

Which is what makes this array usable where the literal union is wanted:

app.ts
const roles = ["admin", "guest"] as const

function go(r: "admin" | "guest") {
  return r
}

console.log(roles.map(go))
Output$ npx tsc --noEmit app.ts$ echo $?0

any

any switches the checker off for that value. Every one of these lines is accepted:

app.ts
const v: any = "ada"

console.log(v.toFixed(2))
console.log(v.anything.at.all)
Output$ npx tsc --noEmit app.ts$ echo $?0

Which only defers the problem to runtime:

app.ts
const v: any = "ada"

try {
  v.toFixed(2)
} catch (err) {
  console.log((err as Error).message)
}
Output$ node app.tsv.toFixed is not a function

Worse, it spreads. data.age is any, so assigning it to a number is not questioned:

app.ts
const data: any = { age: "36" }

const age: number = data.age

console.log(age * 2)
Output$ npx tsc --noEmit app.ts$ node app.ts72

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:

app.ts
const v: unknown = "ada"

console.log(v.toUpperCase())
Output$ npx tsc --noEmit app.tsapp.ts(3,13): error TS18046: 'v' is of type 'unknown'.

It will not quietly become a string either:

app.ts
const v: unknown = "ada"

const s: string = v
Output$ npx tsc --noEmit app.tsapp.ts(3,7): error TS2322: Type 'unknown' is not assignable to type 'string'.

Prove what it is, and it opens up:

app.ts
const v: unknown = "ada"

if (typeof v === "string") {
  console.log(v.toUpperCase())
}
Output$ npx tsc --noEmit app.ts$ echo $?0

never and void

never is the type of a value that never arrives. A function that always throws returns it:

app.ts
function fail(msg: string): never {
  throw new Error(msg)
}

console.log(typeof fail)
Output$ npx tsc --noEmit app.ts$ echo $?0

Nothing is assignable to it, which is the property chapter 3 uses for exhaustiveness:

app.ts
function fail(msg: string): never {
  return msg
}
Output$ npx tsc --noEmit app.tsapp.ts(2,3): error TS2322: Type 'string' is not assignable to type 'never'.

void is different — the function does return, it just has no useful value:

app.ts
function log(msg: string): void {
  console.log(msg)
}

const r: string = log("hi")
Output$ npx tsc --noEmit app.tsapp.ts(5,7): error TS2322: Type 'void' is not assignable to type 'string'.