A quick reference for TypeScript covering basic types, interfaces, generics, utility types, type guards, and module declarations. Keep this handy while writing type-safe code.
| Code / Syntax | Description |
|---|---|
let x: number = 42; | Number type annotation |
let s: string = 'hello'; | String type annotation |
let b: boolean = true; | Boolean type annotation |
let arr: number[] = [1, 2, 3]; | Array type annotation |
let tup: [string, number] = ['a', 1]; | Tuple type |
enum Color { Red, Green, Blue } | Numeric enum |
let val: any = 'anything'; | Any type (opt out of checking) |
let u: unknown = getData(); | Unknown type (safer than any) |
function fail(): never { throw Error(); } | Never type (never returns) |
let v: void = undefined; | Void type (no return value) |
type Literal = 'click' | 'hover'; | String literal type |
| Code / Syntax | Description |
|---|---|
interface User { name: string; age: number; } | Define an interface |
interface User { email?: string; } | Optional property |
interface User { readonly id: number; } | Readonly property |
interface Admin extends User { role: string; } | Extend an interface |
type Point = { x: number; y: number }; | Type alias for object |
type ID = string | number; | Union type alias |
type Pair<T> = { first: T; second: T }; | Generic type alias |
interface Fn { (x: number): boolean; } | Call signature interface |
interface Dict { [key: string]: any; } | Index signature |
type A = X & Y; | Intersection type (combine types) |
| Code / Syntax | Description |
|---|---|
function id<T>(arg: T): T { return arg; } | Generic function |
class Box<T> { value: T; } | Generic class |
function len<T extends { length: number }>(a: T): number | Constrained generic |
interface Repo<T> { get(id: string): T; } | Generic interface |
type Result<T, E = Error> = { data: T } | { error: E }; | Default generic parameter |
function merge<A, B>(a: A, b: B): A & B | Multiple type parameters |
function create<T>(ctor: new () => T): T | Constructor constraint |
type Flatten<T> = T extends any[] ? T[number] : T; | Conditional type with infer |
function pluck<T, K extends keyof T>(obj: T, key: K): T[K] | keyof constraint |
| Code / Syntax | Description |
|---|---|
Partial<User> | Make all properties optional |
Required<User> | Make all properties required |
Readonly<User> | Make all properties readonly |
Pick<User, 'name' | 'email'> | Pick subset of properties |
Omit<User, 'password'> | Omit specific properties |
Record<string, number> | Object type with key/value types |
ReturnType<typeof fn> | Extract function return type |
Parameters<typeof fn> | Extract function parameter types |
Exclude<Union, 'a'> | Remove types from union |
Extract<Union, string> | Extract matching types from union |
NonNullable<string | null> | Remove null and undefined |
| Code / Syntax | Description |
|---|---|
if (typeof x === 'string') { ... } | typeof type guard |
if (x instanceof Date) { ... } | instanceof type guard |
if ('name' in obj) { ... } | in operator narrowing |
function isStr(x: unknown): x is string { ... } | Custom type predicate |
x as string | Type assertion |
value! | Non-null assertion operator |
if (x === null) { ... } | Equality narrowing |
const y = x satisfies Type; | satisfies operator (TS 4.9+) |
switch (action.type) { case 'A': ... } | Discriminated union narrowing |
| Code / Syntax | Description |
|---|---|
declare module 'lib' { export function fn(): void; } | Ambient module declaration |
declare global { interface Window { x: number; } } | Augment global scope |
namespace Utils { export function log() {} } | Namespace declaration |
export type { User }; | Type-only export |
import type { User } from './types'; | Type-only import |
/// <reference types='node' /> | Triple-slash type reference |
declare const __DEV__: boolean; | Ambient variable declaration |
export = legacyModule; | CommonJS-style export |
as const | Const assertion for literal types |
Found this cheat sheet useful? Check out our other references and tools.