π Awaited<Type> (TS 4.5+)
Unwraps Promise types recursively (like await)
type A = Awaited<Promise<string>>;
// string
type B = Awaited<Promise<Promise<number>>>;
// number
type C = Awaited<boolean | Promise<number>>;
// boolean | numberπ Use when working with async functions or .then() chains.
π§© Partial<Type>
Makes all properties optional
interface Todo {
title: string;
description: string;
}
const update = (todo: Todo, data: Partial<Todo>) => ({
...todo,
...data
});π Perfect for update APIs & forms.
π Required<Type>
Makes all properties required
interface Props {
a?: number;
b?: string;
}
const obj: Required<Props> = {
a: 1,
b: "hi"
};π Opposite of Partial
π§ Readonly<Type>
Prevents reassignment of properties
interface Todo {
title: string;
}
const todo: Readonly<Todo> = {
title: "Delete users"
};
// β Error
todo.title = "Edit users";π Great for immutable data & Object.freeze.
π Record<Keys, Type>
Creates object types with fixed keys
type Role = "admin" | "user";
const permissions: Record<Role, boolean> = {
admin: true,
user: false
};π Ideal for maps, configs, and lookup tables.
βοΈ Pick<Type, Keys>
Select specific properties
interface User {
id: number;
name: string;
email: string;
}
type UserPreview = Pick<User, "id" | "name">;π Useful for DTOs & lightweight responses.
π Omit<Type, Keys>
Remove specific properties
type UserSafe = Omit<User, "email">;π Perfect for hiding sensitive fields.
π« Exclude<Union, Members>
Removes types from a union
type Status = "success" | "error" | "loading";
type Active = Exclude<Status, "loading">;
// "success" | "error"π― Extract<Type, Union>
Keeps only matching union members
type Shape =
| { kind: "circle"; r: number }
| { kind: "square"; x: number };
type Circle = Extract<Shape, { kind: "circle" }>;πΏ NonNullable<Type>
Removes null & undefined
type Clean = NonNullable<string | null | undefined>;
// stringπ Safe values only.
π₯ Parameters<Type>
Extracts function parameter types
type Fn = (id: number, name: string) => void;
type Args = Parameters<Fn>;
// [number, string]π ConstructorParameters<Type>
Gets constructor arguments
class User {
constructor(id: number, name: string) {}
}
type Args = ConstructorParameters<typeof User>;
// [number, string]π€ ReturnType<Type>
Extracts return type of a function
const getUser = () => ({ id: 1, name: "Hardik" });
type User = ReturnType<typeof getUser>;π§ InstanceType<Type>
Gets instance type of a class
class Car {
drive() {}
}
type CarInstance = InstanceType<typeof Car>;π NoInfer<Type> (TS 5.4+)
Prevents unwanted inference
function setTheme<T extends string>(
themes: T[],
active: NoInfer<T>
) {}
setTheme(["dark", "light"], "dark"); // β
setTheme(["dark", "light"], "blue"); // βπ§ ThisParameterType<Type>
Extracts this type from a function
function hex(this: Number) {
return this.toString(16);
}
type ThisType = ThisParameterType<typeof hex>;
// NumberβοΈ OmitThisParameter<Type>
Removes this from function type
const fn = hex.bind(10);
// type-safe function without `this`π§© ThisType<Type>
Controls this inside object literals
type Obj<D, M> = {
data: D;
methods: M & ThisType<D & M>;
};π Used in advanced object factories.
π String Utility Types
Uppercase<T>
type A = Uppercase<"hello">;
// "HELLO"Lowercase<T>
type B = Lowercase<"HELLO">;
// "hello"
Capitalize<T>
type C = Capitalize<"hello world">;
// "Hello world"Uncapitalize<T>
type D = Uncapitalize<"Hello">;
// "hello"𧬠Template Literal Types
Create dynamic string types
type Event<T extends string> = `${T}Changed`;
type UserEvents = Event<"name" | "age">;
// "nameChanged" | "ageChanged"π Great for event systems & APIs.
β Pro Tip
Most of these utilities are built using conditional types β mastering them unlocks next-level TypeScript.
