Skip to content

Advanced Type Tools

These are built-in helpers for common type transformations.

interface User {
id: number;
name: string;
email?: string;
}
type UserPatch = Partial<User>;
type FullUser = Required<User>;
type UserPreview = Pick<User, "id" | "name">;
type UserWithoutEmail = Omit<User, "email">;
type UserMap = Record<string, User>;

Mapped types build new types by iterating over keys.

type ReadonlyUser<T> = {
readonly [K in keyof T]: T[K];
};
type ImmutableUser = ReadonlyUser<User>;

Conditional types act like type-level if/else.

type IsString<T> = T extends string ? true : false;
type A = IsString<"hello">; // true
type B = IsString<123>; // false

const config = {
apiUrl: "https://example.com",
timeout: 5000,
};
type Config = typeof config;
type ConfigKeys = keyof Config; // "apiUrl" | "timeout"
  • typeof (in type position) creates a type from a value.
  • keyof gets a union of property names.

infer lets TypeScript extract a type inside a conditional type.

type ReturnTypeOf<T> = T extends (...args: never[]) => infer R ? R : never;
type Fn = (x: number) => string;
type Result = ReturnTypeOf<Fn>; // string

graph TD A[Source Type] --> B[Utility Type] A --> C[Mapped Type] A --> D[Conditional Type] D --> E[infer Extract] A --> F[keyof and typeof]