Implement a type ```IsTuple```, which takes an input type ```T``` and returns whether ```T``` is tuple type. Learn tuple manipulation in this medium-level challenge on TypeScriptPro.
In this medium-level challenge, you'll implement an IsTuple<T> type that determines whether a given type is a tuple type, distinguishing tuples from regular arrays, plain objects with a length property, and never.
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[]> // falseChange the following code to make the test cases pass (no type check errors).
type IsTuple<T> =
[T] extends [never]
? false
: T extends readonly unknown[]
? number extends T['length']
? false
: true
: false;How it works:
[T] extends [never] detects the never type. We wrap T in a tuple to prevent conditional distribution, ensuring never correctly returns false rather than distributing to never.T extends readonly unknown[] checks whether T is an array-like type. Using readonly unknown[] ensures we match both mutable and readonly tuples.number extends T['length']. For regular arrays like number[], the length property is the wide number type, so number extends number is true and we return false. For tuples like [number], the length property is a specific numeric literal (e.g., 1), so number extends 1 is false and we return true.{ length: 1 } fail the T extends readonly unknown[] check, so they correctly return false.This challenge helps you understand the structural differences between tuples and arrays at the type level, and how to apply this knowledge in real-world scenarios.
This challenge is originally from here.
Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.
Only 27 Spots left
Get 1 month early access
Pre-Launch discount