shaKY-iota / type-challenges

Collection of TypeScript type challenges with online judge
https://tsch.js.org/
MIT License
0 stars 0 forks source link

298 - Length of String #22

Open shaKY-iota opened 2 years ago

shaKY-iota commented 2 years ago
// your answers
type LengthOfString<S extends string, Count extends unknown[] = []> = S extends "" ? Count["length"] : S extends `${string}${infer T}` ? LengthOfString<T, [...Count, unknown]> : never
shaKY-iota commented 2 years ago

https://github.com/type-challenges/type-challenges/issues/5519#issue-1090825397

type LengthOfString<S extends string, Count extends unknown[] = []> = S extends `${string}${infer Rest}` ? LengthOfString<Rest, [...Count, unknown]> : Count["length"]