We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
题目链接:LookUp
实现 LookUp,满足下面功能
LookUp
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>>, ]
type LookUp<T, U extends string> = { [K in U]: T extends { type: U } ? T : never; }[U];
T extends { type: U } 会利用分配率
T extends { type: U }
Dog extends { type: "dog" }
true
Dog
Cat extends { type: "cat" }
false
never
得到 Dog | never 的联合类型,由于 never 是所有类型的子类型,所以最后得到 Dog。
Dog | never
type LookUp<T, U extends string> = T extends { type: U } ? T : never;
简化了方法一,不需要通过对象取值
type LookUp<T, U extends string> = T extends (U extends T[keyof T] ? T : never) ? T : never;
方法二中 { type: U },可以写成 U extends T[keyof T] ? T : never
{ type: U }
U extends T[keyof T] ? T : never
type LookUp<T extends { type: any }, U extends string> = T extends unknown ? T["type"] extends U ? T : never : never;
这里 T["type"] extends U 不会运用分配率,会一直输入 never。
T["type"] extends U
为了能够让分配率生效,需要一个前置条件 T extends unknown。
T extends unknown
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目
题目链接:LookUp
实现
LookUp
,满足下面功能答案
方法一
T extends { type: U }
会利用分配率Dog extends { type: "dog" }
=>true
=>Dog
Cat extends { type: "cat" }
=>false
=>never
得到
Dog | never
的联合类型,由于never
是所有类型的子类型,所以最后得到Dog
。方法二
简化了方法一,不需要通过对象取值
方法三
方法二中
{ type: U }
,可以写成U extends T[keyof T] ? T : never
方法四
这里
T["type"] extends U
不会运用分配率,会一直输入never
。为了能够让分配率生效,需要一个前置条件
T extends unknown
。The text was updated successfully, but these errors were encountered: