A quick reference for TypeScript type system features. Covers basic types, interfaces, type aliases, generics, utility types, type guards, enums, and type assertions.
Basic Types
| Type | Example | Notes |
|---|---|---|
| string | let s: string = "hi" | Text values |
| number | let n: number = 42 | Integer and floating point |
| boolean | let b: boolean = true | True or false |
| null | let x: null = null | Explicit null |
| undefined | let u: undefined = undefined | Explicit undefined |
| any | let a: any = "anything" | Opts out of type checking |
| unknown | let u: unknown = getValue() | Type-safe alternative to any |
| never | function fail(): never { throw .. } | Function never returns |
| void | function log(): void {} | No return value |
Interfaces
| Syntax | Description |
|---|---|
| interface User { name: string } | Define object shape |
| interface User { age?: number } | Optional property |
| interface User { readonly id: string } | Read-only property |
| interface Fn { (x: number): string } | Callable interface |
| interface A extends B { } | Extend another interface |
| interface A extends B, C { } | Extend multiple interfaces |
| interface Map { [key: string]: any } | Index signature |
Type Aliases & Unions
| Syntax | Description |
|---|---|
| type ID = string | number | Union type |
| type Point = { x: number; y: number } | Object type alias |
| type Status = "ok" | "err" | String literal union |
| type Both = A & B | Intersection type (combine shapes) |
| type Arr = string[] | Array type alias |
| type Tuple = [string, number] | Tuple type |
| type Fn = (x: number) => string | Function type alias |
| type Nullable<T> = T | null | Generic type alias |
Generics
| Syntax | Description |
|---|---|
| function id<T>(x: T): T | Generic function |
| interface Box<T> { value: T } | Generic interface |
| type Result<T> = { data: T } | Generic type alias |
| function f<T extends HasId>(x: T) | Constrained generic |
| function f<T = string>(x: T) | Default generic type |
| class Stack<T> { push(item: T) {} } | Generic class |
| function f<K extends keyof T>(obj: T, k: K) | Key constraint with keyof |
Utility Types
| Type | Description |
|---|---|
| Partial<T> | Make all properties optional |
| Required<T> | Make all properties required |
| Readonly<T> | Make all properties readonly |
| Pick<T, K> | Select subset of properties by key |
| Omit<T, K> | Remove properties by key |
| Record<K, V> | Object type with keys K and values V |
| ReturnType<F> | Extract return type of function type |
| Parameters<F> | Extract parameter types as tuple |
| Exclude<T, U> | Remove types from union |
| Extract<T, U> | Keep only types assignable to U |
Type Guards
| Pattern | Description |
|---|---|
| typeof x === "string" | Narrow to primitive type |
| x instanceof MyClass | Narrow to class instance |
| "prop" in obj | Narrow by property existence |
| Array.isArray(x) | Narrow to array |
| x !== null && x !== undefined | Narrow out nullish values |
| function isUser(x: any): x is User | Custom type guard function |
| if (x.kind === "circle") | Discriminated union narrowing |
Enums
| Syntax | Description |
|---|---|
| enum Dir { Up, Down } | Numeric enum (0, 1, ...) |
| enum Dir { Up = 1, Down = 2 } | Explicit numeric values |
| enum Color { Red = "RED" } | String enum |
| const enum Status { OK = 200 } | Const enum (inlined at compile) |
| Dir.Up | Access enum member |
| Dir[0] | Reverse mapping (numeric only) |
Type Assertions
| Syntax | Description |
|---|---|
| value as string | Assert type (preferred syntax) |
| <string>value | Angle-bracket assertion (not in JSX) |
| value as unknown as Target | Double assertion (escape hatch) |
| value! | Non-null assertion (remove null/undefined) |
| obj satisfies Type | Validate type without widening |
| as const | Const assertion (literal/readonly types) |