iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Utility Types Ref

Full reference to TypeScript's built-in utility types. They're generic type aliases the compiler ships out of the box.

Object transformation

UtilityEffect
Partial<T>All properties optional.
Required<T>All properties required.
Readonly<T>All properties readonly.
Pick<T, K>Subset by keys.
Omit<T, K>Drop keys.
Record<K, V>Object with K-keyed V values.

Union manipulation

UtilityEffect
Exclude<T, U>T minus matching U members.
Extract<T, U>Keep only members assignable to U.
NonNullable<T>Strip null / undefined.

Function-related

UtilityEffect
Parameters<F>Tuple of parameter types.
ReturnType<F>Return type.
ConstructorParameters<C>Constructor params tuple.
InstanceType<C>Instance type from a class constructor.
ThisParameterType<F>Type of the this parameter.
OmitThisParameter<F>Function type without this.
ThisType<T>Marker for object literals — sets contextual this.

Async

UtilityEffect
Awaited<T>Recursively unwrap Promise.

String manipulation

UtilityEffect
Uppercase<S>'a' → 'A'
Lowercase<S>'A' → 'a'
Capitalize<S>'abc' → 'Abc'
Uncapitalize<S>'Abc' → 'abc'

Examples

TS
type User = { id: number; name: string; email?: string };

type T1 = Partial<User>;                       // { id?: number; name?: string; email?: string }
type T2 = Required<User>;                       // email becomes required
type T3 = Pick<User, 'id' | 'name'>;             // { id: number; name: string }
type T4 = Omit<User, 'email'>;                   // { id: number; name: string }
type T5 = Record<'red' | 'blue', string>;        // { red: string; blue: string }
type T6 = NonNullable<string | null>;            // string
type T7 = ReturnType<() => number>;              // number
type T8 = Parameters<(a: number, b: string) => void>;   // [number, string]
type T9 = Awaited<Promise<Promise<User>>>;       // User
Tip: If you're rolling your own utility type — check if it already exists. The TypeScript team has done a lot of work; reach for the standard before reinventing.

Example

Example
type U = { id: number; name: string; email?: string };

type A = Partial<U>;
type B = Required<U>;
type C = Readonly<U>;
type D = Pick<U, 'id' | 'name'>;
type E = Omit<U, 'email'>;
type F = Record<string, number>;
type G = Awaited<Promise<number>>;
type H = NonNullable<string | null | undefined>;
type I = ReturnType<() => U>;
type J = Parameters<(a: number, b: string) => void>;
console.log('11 utility types ship with TS');
Try it Yourself »

Exercise

Drop a key from T with…

<User, 'email'>

Test yourself

Q1. Strip null / undefined with…
Q2. Pick a function's args as a tuple with…
Q3. Keep certain keys with…

Discussion

Loading…