One page

TypeScript cheat sheet

What to keep at hand. If a line is not clear, go back to the matching lesson in the course.

Open the course: TypeScript Essentials

Types

WhatHow
Literaltype Status = "idle" | "loading"
Objecttype User = { id: string; name?: string }
Arraystring[] / Array<User>
Tupletype Pair = [string, number]
Functiontype Fn = (x: string) => void
Intersectiontype A = B & C

Narrowing

WhatHow
typeofif (typeof x === "string")
equalityif (x.kind === "circle")
inif ("error" in result)
instanceofif (e instanceof Error)
guardfunction isUser(x: unknown): x is User

Utilities

WhatHow
PartialPartial<User>
Pick / OmitPick<User, "id"> Omit<User, "email">
RecordRecord<"a" | "b", number>
keyoftype K = keyof User
typeoftype R = typeof routes
AwaitedAwaited<ReturnType<typeof fn>>

Don't do this

WhatHow
anyturns off checking down the chain
as Userdoesn't validate API data
!user.email! — you promise something that may not be there
Functionwrite (x: T) => R
objectwrite the concrete fields