TypeScript Utility Types: The Ones You Should Actually Know

TypeScript ships with a set of built-in utility types that transform existing types into new shapes. Most TypeScript developers know two or three of them; the full set is genuinely useful. Here are the ones that earn their place.

The Essentials

`Partial`: makes all properties of T optional. `Required`: makes all properties of T required (inverse of Partial). `Readonly`: makes all properties of T readonly — useful for defining immutable data shapes. `Record`: creates an object type with keys of type Keys and values of type Type. Common use: `Record` for a string-keyed number map; `Record<"north" | "south" | "east" | "west", boolean>` for a direction flags object. `Pick`: creates a type by picking a set of properties Keys from T. `Omit`: creates a type by omitting a set of properties Keys from T. The inverse of Pick. `Exclude`: for union types — removes types from T that are assignable to U. `Extract`: for union types — extracts types from T that are assignable to U (inverse of Exclude). `NonNullable`: removes null and undefined from T. `ReturnType`: extracts the return type of a function type T. Very useful when you need the return type of a function but don’t have a separate type definition for it. `Parameters`: extracts the parameters of a function type T as a tuple. `InstanceType`: extracts the instance type of a constructor function type.

Practical Applications

`Partial` for update functions: when writing a function that updates a record, the input typically should allow partial updates — `function updateUser(id: string, changes: Partial)`. More precise than `Partial` when you want to ensure at least one field is present: combine with `Required>`. `Omit` for DTOs: when your API response type should not include certain fields from your internal data model — `type UserDTO = Omit`. `Record` for type-safe maps: when you have a fixed set of keys and want type safety on all values — `type FeatureFlags = Record` where FeatureName is a string literal union. `ReturnType` for derived types: when you want the return type of a function that doesn’t have a separate type declaration — `type ApiResponse = ReturnType`. This creates a tight coupling between the function implementation and the types derived from it. `Extract` and `Exclude` for discriminated unions: when you have a union type and need to narrow it further — `type SuccessAction = Extract`. Conditional types with infer: more advanced utility type patterns involve conditional types and the `infer` keyword — `type UnwrapPromise = T extends Promise ? U : T` extracts the type a Promise resolves to.

Template Literal Types

Less well known but genuinely useful: TypeScript 4.1+ template literal types allow building string types from string literal unions. `type EventName = \`on${Capitalize}\“ creates a type for strings starting with “on” followed by a capitalised string. `type Getters = { [K in keyof T as \`get${Capitalize}\`]: () => T[K] }` creates getter types from any object type. Useful for: defining CSS property names, HTTP header names, event handler names, or other string patterns that have a systematic structure.

上一篇 歌德证书vs TestDaF vs telc:你应该参加哪个德语语言考试?
下一篇 TypeScript实用类型:你实际上应该知道的那些