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: numberNumberlet s: stringStringlet b: booleanBooleanlet a: string[]Array of strings (or Array<string>)let t: [string, number]Tuple — fixed length and typeslet u: unknownUnknown — must be narrowed before uselet v: neverNever — a value that cannot occurObjects & interfaces
interface User { id: number; name: string }Describe an object shapename?: stringOptional propertyreadonly id: numberCannot be reassignedtype User = { id: number }Type alias — same idea, more flexibleinterface Admin extends User {}Extend another interface[key: string]: numberIndex signature for arbitrary keysFunctions
(a: number): string => {}Typed parameters and return(a: number, b = 2) => {}Default parameter(...args: number[]) => {}Rest parameters(): voidReturns nothingtype Fn = (a: string) => numberFunction type aliasUnions & narrowing
type S = 'a' | 'b'Union of literal typesstring | nullValue may be nullA & BIntersection — has both shapestypeof x === 'string'Narrow a union at runtimex as stringType assertion (use sparingly)x!Non-null assertionGenerics & utility types
function f<T>(x: T): TGeneric functionPartial<T>All properties optionalRequired<T>All properties requiredPick<T, "a" | "b">Keep only these keysOmit<T, "a">Remove these keysRecord<string, number>Object with typed keys and valuesReturnType<typeof fn>The return type of a function