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 EssentialsTypes
| What | How |
|---|---|
| Literal | type Status = "idle" | "loading" |
| Object | type User = { id: string; name?: string } |
| Array | string[] / Array<User> |
| Tuple | type Pair = [string, number] |
| Function | type Fn = (x: string) => void |
| Intersection | type A = B & C |
Narrowing
| What | How |
|---|---|
| typeof | if (typeof x === "string") |
| equality | if (x.kind === "circle") |
| in | if ("error" in result) |
| instanceof | if (e instanceof Error) |
| guard | function isUser(x: unknown): x is User |
Utilities
| What | How |
|---|---|
| Partial | Partial<User> |
| Pick / Omit | Pick<User, "id"> Omit<User, "email"> |
| Record | Record<"a" | "b", number> |
| keyof | type K = keyof User |
| typeof | type R = typeof routes |
| Awaited | Awaited<ReturnType<typeof fn>> |
Don't do this
| What | How |
|---|---|
| any | turns off checking down the chain |
| as User | doesn't validate API data |
| ! | user.email! — you promise something that may not be there |
| Function | write (x: T) => R |
| object | write the concrete fields |