Typescript:从类型而不是界面中选择?

sir*_*ver 3 typescript

我有一个类型:

export type MyType = 'something' | 'another' | 'else';
Run Code Online (Sandbox Code Playgroud)

然后我想使用它的一些选项:

Interface MyInterface {
   selection: Pick<MyType, 'something' | 'another'>
}
Run Code Online (Sandbox Code Playgroud)

但出现错误type doesn't satisfy the constraint

难道不能在类型上使用 Pick 而只能在接口上使用吗?

pza*_*ger 8

Pick是关于属性的。您需要使用Extract

type MyType = 'something' | 'another' | 'else';

interface MyInterface {
   selection: Extract<MyType, 'something' | 'another'>;
}
Run Code Online (Sandbox Code Playgroud)