Created
January 24, 2025 01:39
-
-
Save ryangoree/0434c366d495c6cdf5d4d7eefc080f92 to your computer and use it in GitHub Desktop.
Get the length of a string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get the length of a string. | |
* | |
* @example | |
* ```ts | |
* type L = Length<"hello">; // 5 | |
* ``` | |
*/ | |
type Length< | |
T extends string, | |
Counter extends any[] = [], | |
> = T extends `${string}${infer R}` | |
? Length<R, [...Counter, unknown]> | |
: Counter["length"]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment