type Cartoon = { kind: 'cat', name: 'Tom'} | { kind: 'mouse', name: 'Jerry' }
type Animal = 'cat' | 'mouse' // how can I derive this from Cartoon?
我有一个类型的联合类型(卡通),它们都包含密钥" kind"。我想创建一个新类型(动物),它是卡通中包含的所有值的联合类型。
提前致谢!
答案 0 :(得分:2)
您可以使用type query:
type Cartoon = { kind: 'cat', name: 'Tom'} | { kind: 'mouse', name: 'Jerry' }
type Animal = Cartoon['kind'] // Same as 'cat' | 'mouse'