Type alias MakeOptional<T, K>

MakeOptional<T, K>: Simplify<Pick<T, Exclude<RequiredKeys<T>, K>> & Partial<Pick<T, NonRequiredKeys<T> | K>>>

Constructs a new type from T where the specified keys K become optional, while all other keys preserve their original modifiers.

Unlike the naive Omit<T, K> & Partial<Pick<T, K>> form, this version also preserves keys that were already optional in T, which makes it safer for exact type-equality tests and library-grade type transforms.

Type Parameters

  • T extends object

    The original object type.

  • K extends keyof T

    The keys within T that should be made optional.

Example

type User = {
id: string;
name: string;
email: string;
};

type Result = MakeOptional<User, 'email'>;

// {
// id: string;
// name: string;
// email?: string;
// }

Example

type Config = {
host?: string;
port: number;
secure: boolean;
};

type Result = MakeOptional<Config, 'secure'>;

// {
// host?: string;
// port: number;
// secure?: boolean;
// }