TypeScript: Map union type to another union type

You can use conditional type for distributing over the members of the union type (conditional type always takes only one branch and is used only for its distributive property, I
learned this method from this answer)

type A = 'one' | 'two' | 'three';

type Distribute<U> = U extends any ? {type: U} : never;

type B = Distribute<A>;

/*
type B = {
    type: "one";
} | {
    type: "two";
} | {
    type: "three";
}
*/

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)