62 - 查找类型
原创2025/4/29小于 1 分钟
62 - 查找类型
by Anthony Fu (@antfu) #中等 #union #map
题目
有时,您可能希望根据某个属性在联合类型中查找类型。
在此挑战中,我们想通过在联合类型Cat | Dog
中通过指定公共属性type
的值来获取相应的类型。换句话说,在以下示例中,LookUp<Dog | Cat, 'dog'>
的结果应该是Dog
,LookUp<Dog | Cat, 'cat'>
的结果应该是Cat
。
interface Cat {
type: 'cat'
breeds: 'Abyssinian' | 'Shorthair' | 'Curl' | 'Bengal'
}
interface Dog {
type: 'dog'
breeds: 'Hound' | 'Brittany' | 'Bulldog' | 'Boxer'
color: 'brown' | 'white' | 'black'
}
type MyDog = LookUp<Cat | Dog, 'dog'> // expected to be `Dog`
在 Github 上查看:https://tsch.js.org/62/zh-CN
代码
/* _____________ 你的代码 _____________ */
type LookUp<U, T> = U extends { type: T } ? U : never
测试用例
/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
interface Cat {
type: 'cat'
breeds: 'Abyssinian' | 'Shorthair' | 'Curl' | 'Bengal'
}
interface Dog {
type: 'dog'
breeds: 'Hound' | 'Brittany' | 'Bulldog' | 'Boxer'
color: 'brown' | 'white' | 'black'
}
type Animal = Cat | Dog
type cases = [
Expect<Equal<LookUp<Animal, 'dog'>, Dog>>,
Expect<Equal<LookUp<Animal, 'cat'>, Cat>>,
]
相关链接
分享你的解答:https://tsch.js.org/62/answer/zh-CN
查看解答:https://tsch.js.org/62/solutions
更多题目:https://tsch.js.org/zh-CN