env.dev

TypeScript Cheat Sheet — Types & Generics Quick Reference

TypeScript quick reference: basic types, interfaces, generics, utility types, type guards, enums, and type assertions.

Last updated:

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

TypeExampleNotes
stringlet s: string = "hi"Text values
numberlet n: number = 42Integer and floating point
booleanlet b: boolean = trueTrue or false
nulllet x: null = nullExplicit null
undefinedlet u: undefined = undefinedExplicit undefined
anylet a: any = "anything"Opts out of type checking
unknownlet u: unknown = getValue()Type-safe alternative to any
neverfunction fail(): never { throw .. }Function never returns
voidfunction log(): void {}No return value

Interfaces

SyntaxDescription
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

SyntaxDescription
type ID = string | numberUnion type
type Point = { x: number; y: number }Object type alias
type Status = "ok" | "err"String literal union
type Both = A & BIntersection type (combine shapes)
type Arr = string[]Array type alias
type Tuple = [string, number]Tuple type
type Fn = (x: number) => stringFunction type alias
type Nullable<T> = T | nullGeneric type alias

Generics

SyntaxDescription
function id<T>(x: T): TGeneric 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

TypeDescription
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

PatternDescription
typeof x === "string"Narrow to primitive type
x instanceof MyClassNarrow to class instance
"prop" in objNarrow by property existence
Array.isArray(x)Narrow to array
x !== null && x !== undefinedNarrow out nullish values
function isUser(x: any): x is UserCustom type guard function
if (x.kind === "circle")Discriminated union narrowing

Enums

SyntaxDescription
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.UpAccess enum member
Dir[0]Reverse mapping (numeric only)

Type Assertions

SyntaxDescription
value as stringAssert type (preferred syntax)
<string>valueAngle-bracket assertion (not in JSX)
value as unknown as TargetDouble assertion (escape hatch)
value!Non-null assertion (remove null/undefined)
obj satisfies TypeValidate type without widening
as constConst assertion (literal/readonly types)

Frequently Asked Questions

What is the difference between interface and type?

Interfaces can be extended and merged (declaration merging). Type aliases can represent unions, intersections, and mapped types. Use interfaces for object shapes and types for everything else.

What are utility types?

Built-in TypeScript types that transform other types. Common ones: Partial<T> (all optional), Required<T> (all required), Pick<T,K> (subset), Omit<T,K> (exclude keys), Record<K,V> (key-value map).

What is a type guard?

A type guard is a runtime check that narrows a type in a conditional block. Examples: typeof x === "string", x instanceof Date, or custom guards with "is" keyword.