Type Challenge: Implement Length in TypeScript
Solve the Length type challenge in TypeScript: read a tuple's length as a literal type, add the readonly constraint, and test the result with an Equal helper.

The Length challenge asks you to write a generic type that takes a tuple and returns its length as a number literal type. It looks trivial, and the solution is one line, but the reason that line works and the two ways it can fail teach you something real about tuples, readonly and generic constraints. This post states the challenge, gives the solution, walks through why it works, tests it with type assertions, and shows the exact errors you get when you get it wrong. It describes TypeScript 6.x, and the same code behaves identically in the native compiler preview known as tsgo, which is the base of TypeScript 7. The tuple rules are in the handbook chapter on object types.
The challenge
You are given this skeleton and must replace the placeholder so that every test passes.
type Length<T> = any;The expected behaviour is that Length of a tuple type is the number of elements, as a literal. A three-element tuple gives 3, and an empty tuple gives 0. Passing something that is not an array type should be a compile error, because a string or a number has no tuple length to read.
The challenge is usually run with as const arrays, so the tuples that come in are readonly. That detail is where most first attempts fail, and we will come back to it.
The solution
type Length<T extends readonly unknown[]> = T["length"];That is the whole answer. T["length"] is an indexed access type. It asks for the type of the length property of T. For an ordinary array type such as number[], the length property is the type number. For a tuple type, the compiler knows exactly how many elements there are, and it gives the length property a literal type instead.
A quick check shows the difference.
type A = Length<[1, 2, 3]>;
type B = Length<number[]>;
type C = Length<[]>;
const a: A = 4;length.ts(6,7): error TS2322: Type '4' is not assignable to type '3'.The error tells you that A is the literal type 3. If you hover over B in an editor you will see number, and C is 0. The same solution handles all three, because the information is already in the type of the tuple.
Why it works
A tuple type in TypeScript is an array type with a fixed number of positions. The type [1, 2, 3] is not just an array of numbers. It records that position 0 holds 1, position 1 holds 2, and position 2 holds 3, and that there are no other positions. The compiler models this by giving the tuple a length property whose type is the literal type of the element count, which here is 3.
The indexed access T["length"] reads that property at the type level. There is no counting to do and no recursion to write. People who have not seen this often reach for a recursive conditional type that peels elements off the front and counts them with an accumulator. That works, and it is a good exercise in its own right, but it is far more code than the language needs, and it hits the recursion depth limit sooner on long tuples. The built-in length property is simpler and faster to check.
Optional elements and rest elements change the answer in the way you would expect. A tuple such as [1, 2?] has a length of 1 | 2, because it can have either one or two elements. A tuple with a rest element such as [1, ...string[]] has a length of number, because the count is open ended. Both results come directly from the property, so the solution gets them without any extra logic.
type D = Length<[1, 2?]>;
type E = Length<[1, ...string[]]>;
const d: D = 3;
const e: E = "x";length.ts(4,7): error TS2322: Type '3' is not assignable to type '2 | 1'.
length.ts(5,7): error TS2322: Type 'string' is not assignable to type 'number'.The first error shows the union 2 | 1, and the second shows that an open ended tuple gives plain number. TypeScript prints the union members in its internal order, which is why 2 comes before 1.
Why the constraint matters
The constraint T extends readonly unknown[] does two jobs. It limits the input to array-like types, and it makes T["length"] a legal expression. Without a constraint, the compiler cannot know that T has a length property, so it rejects the indexed access.
type Bad<T> = T["length"];length.ts(1,16): error TS2536: Type '"length"' cannot be used to index type 'T'.With the constraint in place, a non-array argument fails at the call site, where the message is much more useful to the person using your type.
type F = Length<"hello">;length.ts(1,17): error TS2344: Type 'string' does not satisfy the constraint 'readonly unknown[]'.That error is what the challenge means by a compile error for invalid input. Note that a string does have a length property at run time, but the type challenge wants tuples, and the constraint is how you say so.
Why readonly is in the constraint
This is the part that catches people. Many versions of the solution on the internet write T extends any[] without readonly. That fails the moment you pass a tuple declared with as const, because as const makes the tuple readonly.
type Length<T extends any[]> = T["length"];
const tesla = ["tesla", "model 3", "model X", "model Y"] as const;
type G = Length<typeof tesla>;length.ts(4,17): error TS2344: Type 'readonly ["tesla", "model 3", "model X", "model Y"]' does not satisfy the constraint 'any[]'.
The type 'readonly ["tesla", "model 3", "model X", "model Y"]' is 'readonly' and cannot be assigned to the mutable type 'any[]'.A readonly array cannot be assigned to a mutable array type, because the mutable type would let the receiver push, pop and assign to indices, and the readonly value promises that nobody will. The fix is to widen the constraint to readonly unknown[]. A mutable array is assignable to a readonly one, so the wider constraint accepts both kinds of tuple. This is a general rule worth remembering: when you write a type that only reads from an array, constrain it to the readonly form, and you will accept more inputs for free.
The choice of unknown over any in the constraint is a style point. Both accept every array. unknown[] avoids the any that lint rules often ban, and since the solution never looks at the element type, nothing is lost.
Testing with type assertions
Type challenges are checked with a pair of helpers. Equal compares two types for exact identity, and Expect fails to compile unless its argument is true.
type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2)
? true
: false;
type Expect<T extends true> = T;
type Length<T extends readonly unknown[]> = T["length"];
const tesla = ["tesla", "model 3", "model X", "model Y"] as const;
const spaceX = [
"FALCON 9",
"FALCON HEAVY",
"DRAGON",
"STARSHIP",
"HUMAN SPACEFLIGHT",
] as const;
type cases = [
Expect<Equal<Length<typeof tesla>, 4>>,
Expect<Equal<Length<typeof spaceX>, 5>>,
Expect<Equal<Length<[]>, 0>>,
Expect<Equal<Length<[1, 2, 3]>, 3>>,
];(no output: the file compiles without errors)If the solution were wrong, one of the entries would fail. Suppose a version returned number for every input. The first case would then read Expect<Equal<number, 4>>, and Equal would produce false.
length.test.ts(18,10): error TS2344: Type 'false' does not satisfy the constraint 'true'.That is the failure signal you look for in any type challenge. The test passes when the file compiles and fails when Expect receives false.
The reason Equal is written with the deferred function trick, and not with a simple two-way extends, is that the simple check treats some different types as equal, for instance any and other types. The function form compares the types as they would be used in an identical position, which is stricter and is the form the type challenge repository uses.
Using Length in real code
A tuple length type is useful anywhere a function has fixed arity. A typed zip helper, a matrix type that carries its dimensions, and a function that must be called with a number of arguments matching a list all benefit from reading the length as a literal.
type Length<T extends readonly unknown[]> = T["length"];
function rowCount<const T extends readonly unknown[]>(rows: T): Length<T> {
return rows.length as Length<T>;
}
const n = rowCount(["a", "b", "c"]);
const check: 3 = n;(no output: the file compiles without errors)The const modifier on the type parameter, available since TypeScript 5.0, asks the compiler to infer the argument as a readonly tuple of literals and not as string[]. Without it, n would have type number. The cast on the return line is needed because the compiler cannot prove that the run-time length property has the type-level answer, and that is one place where a type-level result is a promise rather than a proof.
Common mistakes
The first mistake is leaving off readonly in the constraint and then hitting the error shown above when the input comes from as const. The second is writing a recursive solution when the built-in property does the job. The third is forgetting that a non-tuple array gives number, which makes Length useless for checking an exact count on a value typed as string[]. The fourth is expecting Length<string> to give the character count. Strings have a run-time length, but the type system does not count characters, and a string literal type does not carry a numeric length.
When this does not apply
If you only need the length of a runtime value, write value.length and ignore the type. The type is for code that needs to constrain, compute or compare lengths at compile time, for example to require two lists to have the same size. In that case, compare Length<A> with Length<B> in a conditional type. For arrays of unknown size, the answer is always number, and no type-level trick will change that.


