utilitypes-ts
    Preparing search index...

    Type Alias MakeOptional<T, K>

    MakeOptional: T extends T
        ? Omit<T, K> & Partial<Pick<T, Extract<K, keyof T>>>
        : never

    Makes specified keys K optional in each member of a union T.

    This distributes over unions, ensuring each member is processed individually. Only keys that exist in each member are affected.

    Type Parameters

    • T

      The target type (can be a union)

    • K extends UnionKey<T> = UnionKey<T>

      Keys to make optional (defaults to all union keys)

    type Result = MakeOptional<{ a: string; b: number }>;
    // ^?
    // { a?: string; b?: number }
    type Result = MakeOptional<{ a: string } | { b: number }>;
    // ^?
    // { a?: string } | { b?: number }
    type Result = MakeOptional<{ a: string } | { b: number }, "a">;
    // ^?
    // { a?: string } | { b: number }