-
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 练习题第三十九题 #59
Comments
type IsAny<T> = 0 extends 1 & T ? true : false;
type I0 = IsAny<never>; // false
type I1 = IsAny<unknown>; // false
type I2 = IsAny<any>; // true 思路: 利用任何类型和any交叉都等于any来实现。 |
// unknown 只能赋给 unknown 或者 any
type IsAny<T> = [unknown] extends [T] ? ([T] extends [string] ? true : false) : false
type I0 = IsAny<never> // false
type I1 = IsAny<unknown> // false
type I2 = IsAny<any> // true |
type IsAny = [unknown] extends [T] type I0 = IsAny // false |
// 解法: any的特性是与任意值并得出的值为本身
// 我们随便找两个不同的值即可
type IsAny<T> = 'a' extends 'b' & T ? true : false |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
实现
IsAny
工具类型,用于判断类型T
是否为any
类型。具体的使用示例如下所示:The text was updated successfully, but these errors were encountered: