elefcode
← All cheat sheets

Languages

TypeScript Cheat Sheet — Types Reference

The TypeScript syntax you use most, from basic annotations to generics and utility types.

Put it into practice with the matching tool.

Generate TypeScript from JSON →

Advertisement

Basic types

let n: numberNumber
let s: stringString
let b: booleanBoolean
let a: string[]Array of strings (or Array<string>)
let t: [string, number]Tuple — fixed length and types
let u: unknownUnknown — must be narrowed before use
let v: neverNever — a value that cannot occur

Objects & interfaces

interface User { id: number; name: string }Describe an object shape
name?: stringOptional property
readonly id: numberCannot be reassigned
type User = { id: number }Type alias — same idea, more flexible
interface Admin extends User {}Extend another interface
[key: string]: numberIndex signature for arbitrary keys

Functions

(a: number): string => {}Typed parameters and return
(a: number, b = 2) => {}Default parameter
(...args: number[]) => {}Rest parameters
(): voidReturns nothing
type Fn = (a: string) => numberFunction type alias

Unions & narrowing

type S = 'a' | 'b'Union of literal types
string | nullValue may be null
A & BIntersection — has both shapes
typeof x === 'string'Narrow a union at runtime
x as stringType assertion (use sparingly)
x!Non-null assertion

Generics & utility types

function f<T>(x: T): TGeneric function
Partial<T>All properties optional
Required<T>All properties required
Pick<T, "a" | "b">Keep only these keys
Omit<T, "a">Remove these keys
Record<string, number>Object with typed keys and values
ReturnType<typeof fn>The return type of a function

Related guides

More cheat sheets