devtypes - v1.1.0
    Preparing search index...

    Type Alias DiscriminatedUnion<Tag, Map>

    DiscriminatedUnion: { [K in keyof Map & string]: { [P in Tag]: K } & Map[K] }[keyof Map & string]

    Build a discriminated union from a tag key and a mapping object.

    Type Parameters

    • Tag extends string

      Discriminator property name

    • Map extends Record<string, any>

      Mapping of discriminator values to object shapes

    Creates a union of object types where each member is tagged with a literal discriminator value derived from the mapping keys.

    type AnimalMap = {
    cat: { meows: boolean };
    dog: { barks: boolean };
    };

    type Animal = DiscriminatedUnion< 'type', AnimalMap >;
    // { type: 'cat'; meows: boolean } | { type: 'dog'; barks: boolean }