Curtesy to vyenkv answer.
type LengthOf <
String extends string ,
Sum extends 0 [] = []
> = String extends `${ string }${ infer $Rest }`
? LengthOf< $Rest , [ ... Sum , 0 ] >
: Sum[ 'length' ]
type HasLength <
String extends string ,
Length extends number
> = LengthOf<String> extends Length
? true
: false
type False = HasLength<'Hello',4>
type True = HasLength<'World',5>type LengthOf < //
String extends string , //
Sum extends 0 [] = [] // Tuple of zeros
> = String extends `${ string }${ infer $Rest }` // Destructures string into it's first char + remainder
? LengthOf< $Rest , [ ... Sum , 0 ] > // Counts the length of the remainder + adds zero to tuple
: Sum[ 'length' ] // If the end is reached, returns length of tuple