Type Lookup
问题描述
有时,您可能希望根据某个属性在联合类型中查找类型。
在此挑战中,我们想通过在联合类型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`
// ============= Test Cases =============
import type { Equal, Expect } from './test-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>>
]
// ============= Your Code Here =============
// 答案1
type LookUp<U, T> = U extends { type: T } ? U : never
// 答案2
type LookUp<U extends { type: string }, T extends U['type']> = U extends {
type: T
}
? U
: never
// 错误答案
type LookUp<T extends { type: string }, U extends string> = T['type'] extends U ? T : never
// 答案3
type LookUp<T extends { type: string }, U extends string> = T extends unknown
? T['type'] extends U
? T
: never
: never
这道题首先想到的是,两个泛型参数的范围,第一个泛型参数肯定继承自对象,第二个泛型参数应该是对象中的属性 type 的值,首先约束第一个参数 T extends { type: string },第二个泛型参数的类型应该是字符串 U extends string,最后返回泛型 U 即可,算是一道简单题,不过这里更详细的写法应该是 答案2,更小范围的约束泛型,至于答案3,我暂时也没有明白,为什么加了一层 T extends unknown 判断就可以了。