Skip to content

Instantly share code, notes, and snippets.

@ryangoree
Created January 24, 2025 01:39
Show Gist options
  • Save ryangoree/0434c366d495c6cdf5d4d7eefc080f92 to your computer and use it in GitHub Desktop.
Save ryangoree/0434c366d495c6cdf5d4d7eefc080f92 to your computer and use it in GitHub Desktop.
Get the length of a string.
/**
* 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