Skip to content
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 中判断的问题 #9

Open
xiaochengzi6 opened this issue Feb 23, 2023 · 0 comments
Open

ts 中判断的问题 #9

xiaochengzi6 opened this issue Feb 23, 2023 · 0 comments

Comments

@xiaochengzi6
Copy link
Owner

  1. IsAny
    如果是两个条件类型 T1 extends U1 ? X1 : Y1 和 T2 extends U2 ? X2 : Y2 相关的话,那 T1 和 T2 相关、X1 和 X2 相关、Y1 和 Y2 相关,而 U1 和 U2 相等。
type IsAny<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false 
  1. IsNever
    never 在条件类型中也比较特殊,如果条件类型左边是类型参数,并且传入的是 never,那么直接返回 never, 直接去判断的话就会出现这样的问题,使用数组包一下可以避免
type IsNever<A, B> = [T] extends [never] ? true : false 
  1. IsAny
    any 在条件类型中也比较特殊,如果类型参数为 any,会直接返回 trueType 和 falseType 的合并:
type TestAny<T> = T extends number ? 1 : 2;
  1. IsTuple
    元组类型的 length 是数字字面量,而数组的 length 是 number。
type IsTuple<T> = 
  T extends [...params: infer Eles]
   ? NotEqual<Eles['length'], number>
   : false
  

type NotEqual<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? false : true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant