TypeScript Cheatsheet

TypeScript types, generics, utility types, and patterns with examples

The TypeScript cheatsheet covers types, interfaces, generics, utility types, and patterns. Find the syntax you need instantly — from basic primitives to advanced mapped types. Includes copy buttons on every example.

How to Use This TypeScript Cheatsheet

TypeScript adds static types to JavaScript, catching errors at compile time instead of runtime. This cheatsheet covers the core type system features you'll use daily, from basic primitives to advanced utility types.

Type Annotations

TypeScript uses colon syntax for type annotations: let name: string = "Alice". In most cases TypeScript infers the type automatically — annotations are most useful for function parameters, return types, and complex object shapes.

Interfaces vs Types

Both define object shapes. Interfaces support declaration merging and extending. Type aliases are more flexible — they can represent unions, intersections, and mapped types. The general rule: use interfaces for public APIs and class shapes, use types for everything else.

Generics

Generics let you write type-safe code that works across multiple types. The T in function identity<T>(arg: T): T is a type parameter — it's filled in when the function is called. Constrain generics with T extends SomeType to ensure they have certain properties.

Utility Types

TypeScript ships with powerful utility types that transform existing types: Partial makes all properties optional, Required makes all mandatory, Pick selects specific keys, Omit excludes keys, Record creates an indexed object type. These save you from rewriting similar type definitions.

Frequently Asked Questions

Is this TypeScript cheatsheet free?

Yes, completely free with no signup. All content runs in your browser.

What is the difference between interface and type in TypeScript?

Both define object shapes, but interfaces are extendable (via extends and declaration merging) while types are more flexible (unions, intersections, mapped types, conditional types). Prefer interfaces for object shapes that might be extended; use types for unions, intersections, or utility type combinations.

What are TypeScript utility types?

Utility types are built-in generic types that transform existing types. Partial<T> makes all properties optional, Required<T> makes all required, Pick<T, K> selects keys, Omit<T, K> excludes keys, Record<K, V> creates an object type, ReturnType<F> extracts a function's return type, Awaited<T> unwraps Promise types.

What is type narrowing in TypeScript?

Type narrowing is when TypeScript refines a type within a conditional block. Using typeof (for primitives), instanceof (for classes), 'in' operator (for properties), or custom type guard functions (foo is Type) lets TypeScript know the exact type inside that branch, enabling type-safe operations without casts.

What are TypeScript generics used for?

Generics let you write reusable code that works with any type while still being type-safe. Instead of using 'any', you define a type parameter T that gets filled in at call time. For example, function identity<T>(arg: T): T works for strings, numbers, objects — whatever is passed in.