-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
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
「重学TS 2.0 」TS 练习题第四十四题 #65
Comments
type Types = string | number | symbol;
type Unpacked<T> = T extends Types
? T
: T extends (...arg: any) => infer R
? R
: T extends Array<infer A>
? A
: T extends Promise<infer P>
? P
: never;
type T00 = Unpacked<string>; // string
type T01 = Unpacked<string[]>; // string
type T02 = Unpacked<() => string>; // string
type T03 = Unpacked<Promise<string>>; // string
type T04 = Unpacked<Unpacked<Promise<string>[]>>; // string
type T05 = Unpacked<any>; // any
type T06 = Unpacked<never>; // never 我这个实现实话说很不满意,感觉完全是照着测试用例再写 |
type Unpacked = T extends (...args: any) => infer A // 你的实现代码 type T00 = Unpacked; // string |
type Unpacked<T> = T extends Promise<infer V>
? Unpacked<V>
: T extends () => void
? ReturnType<T>
: T extends unknown[]
? T[number]
: T; |
// 疑问 没有理解到他们的写法 |
实现
Unpacked
工具类型,用于对类型执行 “拆箱” 操作。具体的使用示例如下所示:The text was updated successfully, but these errors were encountered: