Implement a type LengthOfString<S>
that calculates the length of the template string (as in 298 - Length of String):
type T0 = LengthOfString<"foo"> // 3
The type must support strings several hundred characters long (the usual recursive calculation of the string length is limited by the depth of recursive function calls in TS, that is, it supports strings up to about 45 characters long).
// Run in batch of 10
type LengthOfString<S extends string, ACC extends unknown[] = []> = S extends `${string}${string}${string}${string}${string}${string}${string}${string}${string}${string}${infer REST}`
? LengthOfString<REST, [...ACC, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown]>
: S extends `${string}${infer REST}`
? LengthOfString<REST, [...ACC, unknown]>
: ACC['length']
/* _____________ Test Cases _____________ */
import type { Equal, IsTrue } from '@type-challenges/utils'
type cases = [
IsTrue<Equal<LengthOfString<''>, 0>>,
IsTrue<Equal<LengthOfString<'1'>, 1>>,
IsTrue<Equal<LengthOfString<'12'>, 2>>,
IsTrue<Equal<LengthOfString<'123'>, 3>>,
IsTrue<Equal<LengthOfString<'1234'>, 4>>,
IsTrue<Equal<LengthOfString<'12345'>, 5>>,
IsTrue<Equal<LengthOfString<'123456'>, 6>>,
IsTrue<Equal<LengthOfString<'1234567'>, 7>>,
IsTrue<Equal<LengthOfString<'12345678'>, 8>>,
IsTrue<Equal<LengthOfString<'123456789'>, 9>>,
IsTrue<Equal<LengthOfString<'1234567890'>, 10>>,
IsTrue<Equal<LengthOfString<'12345678901'>, 11>>,
IsTrue<Equal<LengthOfString<'123456789012'>, 12>>,
IsTrue<Equal<LengthOfString<'1234567890123'>, 13>>,
IsTrue<Equal<LengthOfString<'12345678901234'>, 14>>,
IsTrue<Equal<LengthOfString<'123456789012345'>, 15>>,
IsTrue<Equal<LengthOfString<'1234567890123456'>, 16>>,
IsTrue<Equal<LengthOfString<'12345678901234567'>, 17>>,
IsTrue<Equal<LengthOfString<'123456789012345678'>, 18>>,
IsTrue<Equal<LengthOfString<'1234567890123456789'>, 19>>,
IsTrue<Equal<LengthOfString<'12345678901234567890'>, 20>>,
IsTrue<Equal<LengthOfString<'123456789012345678901'>, 21>>,
IsTrue<Equal<LengthOfString<'1234567890123456789012'>, 22>>,
IsTrue<Equal<LengthOfString<'12345678901234567890123'>, 23>>,
IsTrue<Equal<LengthOfString<`aaaaaaaaaaaaggggggggggggggggggggkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaggggggggggggggggggggkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaggggggggggggggggggggkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaggggggggggggggggggggkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`>, 1088>>,
]
标签:String,IsTrue,Length,109,type,LengthOfString,string From: https://www.cnblogs.com/Answer1215/p/16901969.html