What does the following recursive type compute? type TupleLength = T extends [any, …infer Rest] ? TupleLength : Acc[‘length’];

TypeScript Professional Hard

TypeScript Professional — Hard

What does the following recursive type compute? type TupleLength = T extends [any, …infer Rest] ? TupleLength : Acc[‘length’];

Key points

  • Conditional types are used to check if T extends a tuple with a head element and infer the rest.
  • The recursion continues until the tuple is fully consumed, returning the length as a numeric literal type.
  • Acc['length'] is not used to track the length of the tuple.

Ready to go further?

Related questions