Back to Cheatsheets
TypeScript

Generics

Reusable type-safe components

Generic Functions

3 items
CommandDescription
function identity<T>(arg: T): T
Generic function declaration
const box = <T>(val: T) => ({ val })
Generic arrow function
identity<string>("hello")
Explicit type argument

Generic Interfaces

2 items
CommandDescription
interface Response<T> { data: T; error?: string }
Generic interface
type Array<T> = T[]
Built-in generic

Constraints

3 items
CommandDescription
<T extends { id: string }>
Must have id property
keyof T
Union of keys of T
T[K]
Access type of property K