Skip to content

Menu

TypeScript Types Cheat Sheet: Utility Types, Narrowing and Generics

Checked against TypeScript TypeScript 7.0.2 and 5.9.3 (examples compiled with both) on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for developers who write TypeScript every day but still look up how Omit differs from Exclude or why a union check doesn't narrow. It covers the type system only, not compiler options. Every example in the rows and the worked example was compiled with tsc --strict on TypeScript 7.0.2 (the Go-based compiler) and on 5.9.3, the version this site's repo uses, and both gave the same errors.

When to use what

Start with narrowing, not assertions. A switch on a literal kind or status field (a discriminated union) gives you exact types in each branch and, with a never check in the default case, a compile error when someone adds a new variant. as just tells the compiler to trust you.

Take unknown for anything that comes from outside: JSON, catch variables, message events. You can't call methods on it until you narrow it, which is the point. any switches checking off for everything it touches and spreads through return types. Keep any for migration shims, and grep for it later.

Use satisfies when you want the compiler to check an object against a type but keep the literal types it inferred. Use an annotation when you want the wider type on purpose, for example a config that will be mutated later.

For utility types, Pick and Omit work on object keys, while Extract and Exclude work on union members; mixing them up is the most common error we see in reviews. If you are typing an API response, generate a first draft with JSON to TypeScript, and if you also need runtime checks, JSON to Zod gives you a schema you can infer types from. Our generics guide and utility types post go deeper than a cheat sheet can.

Version numbers are when each type was added, from the utility types reference.

Partial<User>

Make every property optional; good for PATCH bodies (2.1)

Required<T> / Readonly<T>

Remove ? from every property, or mark every property readonly

From the docs, not run by us
Pick<User, "id" | "name">

Keep only the listed keys of an object type

Omit<User, "id">

Drop the listed keys; combine as Partial<Omit<User, "id">> for an update type

Record<Keys, V>

Object type with known keys (or string) mapped to one value type

Exclude<"a" | "b" | "c", "a">

Remove union members; this gives "b" | "c"

Extract<T, U>

Keep only the union members assignable to U

From the docs, not run by us
NonNullable<T>

Remove null and undefined from a type

ReturnType<typeof f> / Parameters<typeof f>

Function return type, or its parameters as a labelled tuple such as [a: number, b: string]

Awaited<Promise<Promise<number>>>

Unwrap promises recursively; this gives number (4.5)

NoInfer<T>

Stop a parameter position from widening a type argument; pick(["a","b"], "c") became an error (5.4)

typeof x === "string"

Narrow primitives; remember typeof null is "object", so check for null as well

"name" in x

Narrow by property; since 4.9 it also works on object, adding Record<"name", unknown>

x instanceof Date

Narrow by class via the prototype chain

From the docs, not run by us
switch (o.status) { … }

Discriminated union: a shared literal field narrows to the matching variant in each case

const _x: never = o;

Exhaustiveness check in default; an unhandled variant gives error TS2322 'not assignable to type never'

function isString(x: unknown): x is string

Type predicate; callers get string in the true branch

value != null

Loose inequality removes both null and undefined in one check

From the docs, not run by us
{ port: 3000 } satisfies Record<string, string | number>

Check the object but keep port typed as number; the annotated version gave string | number

let v: unknown

Accepts any value but blocks member access until narrowed (error TS18046 'is of type unknown')

let v: any

Disables checking; anyVal.foo.bar() compiled with no error at all

function fail(): never

Return type for functions that always throw or never finish

From the docs, not run by us
object vs Object vs {}

object means any non-primitive; the handbook says to always use object, not Object

From the docs, not run by us
<T extends { length: number }>

Constrain a type parameter; passing 42 failed with TS2345

<T, K extends keyof T>(obj: T, key: K): T[K]

Tie a key argument to the object's keys; a typo is a compile error

<T extends HTMLElement = HTMLDivElement>

Default type argument, used when it is omitted or can't be inferred

From the docs, not run by us
keyof User

Union of an object type's keys

User["role"]

Indexed access: the type of one property; you must index with a type, not a const

(typeof roles)[number]

Element type of an as const array; gave "admin" | "user" | "guest" here

Conditional type rows follow the conditional types handbook page.

{ [K in keyof T]: boolean }

Mapped type: build a new object type from every key of T

{ -readonly [K in keyof T]-?: T[K] }

Strip readonly and ? from every property

{ [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K] }

Rename keys with as and template literals (4.1+)

From the docs, not run by us
[K in keyof T as Exclude<K, "kind">]

Drop a key by mapping it to never

From the docs, not run by us
T extends string ? A : B

Conditional type; picks a branch based on assignability

From the docs, not run by us
T extends Array<infer I> ? I : T

Use infer to pull a type out of another type

From the docs, not run by us
[T] extends [any] ? T[] : never

Wrap in brackets to stop distribution: gave (string | number)[] instead of string[] | number[]

Worked example: Make the compiler catch a new order status you forgot to handle

Start with a union where every variant has a literal status field, and add a never check in the default branch:

type Order =
  | { status: "pending"; createdAt: Date }
  | { status: "paid"; paidAt: Date; amount: number }
  | { status: "refunded"; refundId: string };

function label(o: Order): string {
  switch (o.status) {
    case "pending": return "waiting since " + o.createdAt.toISOString();
    case "paid": return `paid ${o.amount}`;
    default: {
      const unhandled: never = o;
      return unhandled;
    }
  }
}

Run the compiler:

$ tsc --noEmit --strict types.ts
types.ts(11,13): error TS2322: Type '{ status: "refunded"; refundId: string; }' is not assignable to type 'never'.

We got this exact line from both TypeScript 7.0.2 and 5.9.3. Inside each case, o is narrowed to one variant, which is why o.amount is allowed only under "paid". By the time control reaches default, the only variant left is the refunded one, and it can't be assigned to never, so the error names it for you. Add a case "refunded" and the error goes away. Next month, when someone adds a "cancelled" status, this function fails the build instead of silently returning undefined.

Common mistakes

  • Using Omit on a union of string literals, or Exclude on an object type.

    Omit and Pick take object keys; Exclude and Extract filter union members. Pick the pair that matches what you hold.

  • Checking typeof x === "object" and assuming x is not null.

    Add x !== null (or a truthiness check), because typeof null is also "object".

  • Annotating a config object with Record<string, string | number> and then losing the literal types.

    Use satisfies instead of an annotation so the check happens but each property keeps its inferred type.

  • Typing parsed JSON or a catch variable as any.

    Use unknown and narrow with typeof, in or a type predicate before using it.

  • Writing Person[key] where key is a const variable.

    Index with a type: Person[typeof key] or a type alias. The compiler's error message suggests typeof key itself.

  • Being surprised that a conditional type returns string[] | number[] for a union input.

    That is distribution over a naked type parameter. Wrap both sides in brackets, [T] extends [any], when you want the union treated as one type.