TypeScript:将联合类型映射到另一个联合类型

TypeScript: Map union type to another union type

是否可以在 TypeScript 中将一个联合类型映射到另一个联合类型?

我希望能够做什么

例如给定联合类型 A:

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

我希望能够将其映射到联合类型 B:

type B = { type: 'one' } | { type: 'two'} | { type: 'three' };

我试过的

type B = { type: A };

但这会导致:

type B = { type: 'one' | 'two' | 'three' };

这不是我想要的。

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 属性, 我 这个方法是从)

学来的
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";
}
*/