typescript实现Includes

type isPillarMen = Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Dio'>; 
// expected to be `false`
// 标准 Equal 判断逻辑,具体原因看 Equal判断 章节
type MyEqual<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B
  ? 1
  : 2
  ? true
  : false;

type Includes<T extends readonly any[], U> = T extends [infer F, ...infer R]
  ? MyEqual<F, U> extends true
    ? true
    : // 递归判断剩余元素
      Includes<R, U>
  : false;

猜你喜欢

转载自blog.csdn.net/weixin_56356934/article/details/140471803