298 - Length of String

Author: VarunMendreCreated Sep 21, 2026Updated Sep 21, 2026
Labelsansweren298
typescript
// Recursively takes the first character from the string,
// stores each character in a tuple, and returns the tuple length.
type LengthOfString<S extends string, T extends string[] = []> =
  S extends `${infer F}${infer L}`
    ? LengthOfString<L, [...T, F]>
    : T['length'];

Source: type-challenges/type-challenges