[Typescript] 66. Medium - IsTuple

Implement a type IsTuple, which takes an input type T and returns whether T is tuple type.

For example:

type case1 = IsTuple<[number]> // true
type case2 = IsTuple<readonly [number]> // true
type case3 = IsTuple<number[]> // false

 

/* _____________ Your Code Here _____________ */

type IsTuple<T> = [T] extends [never] 
  ? false
  : T extends readonly any[] 
    ? any[] extends T 
      ? false
      : true
    : false;
type x = never extends any[] ? true: false
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<IsTuple<[]>, true>>,
  Expect<Equal<IsTuple<[number]>, true>>,
  Expect<Equal<IsTuple<readonly [1]>, true>>,
  Expect<Equal<IsTuple<{ length: 1 }>, false>>,
  Expect<Equal<IsTuple<number[]>, false>>,
  Expect<Equal<IsTuple<never>, false>>,
]

 

posted @ 2022-10-23 20:45  Zhentiw  阅读(17)  评论(0编辑  收藏  举报