utilitypes-ts
    Preparing search index...

    Type Alias StrictPick<T, K>

    StrictPick: T extends T ? Pick<T, Extract<K, keyof T>> : never

    Strictly picks keys K from each member of a union T.

    This behaves like Pick, but distributes over unions and only allows keys that actually exist in the union. Passing an invalid key will result in a type error.

    Type Parameters

    • T

      The target type (can be a union)

    • K extends UnionKey<T>

      Keys to pick (must exist in UnionKey<T>)

    type Invalid = StrictPick<
    { a: string },
    //@ts-expect-error "b" is not a key of the given type
    "b"
    >;
    type Result = StrictPick<
    | { type: "a"; name: string }
    | { type: "b"; age: number },
    "type" | "name"
    >;
    // ^?
    // | { type: "a"; name: string; }
    // | { type: "b"; }