如何在typescript联合类型[duplicate]中省略值

ryoqjall  于 2023-02-20  发布在  TypeScript
关注(0)|答案(1)|浏览(81)
    • 此问题在此处已有答案**:

Is there a method to directly exclude types from a union type in Typescript?(1个答案)
TypeScript Filter out types from type(1个答案)
1年前关闭。
我喜欢的类型:

export type Example = ExampleOne | ExampleTwo | ExampleThree;

如何创建一个只包含ExampleOneExampleTwo的新类型ExampleX
我试过:

export type ExampleX = Omit<Example, 'ExampleThree'>;

但那不管用?

uxh89sit

uxh89sit1#

您要查找的是Exclude,而不是Omit

    • 一米二米一x**

通过从UnionType中排除所有可分配给ExcludedMembers的联合成员来构造类型。

  • (你不想被引用)*

因此:

export type ExampleX = Exclude<Example, ExampleThree>;

Playground链接

相关问题