Last active
February 17, 2024 21:16
-
-
Save aparx/b24c68ffbf1c7b52f15c8ce66b73f7d9 to your computer and use it in GitHub Desktop.
Typesafe string concat
This file contains 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
export type StringConcat<TArgs extends string[]> = _Flatten<TArgs>; | |
type _Flatten<TArgs extends string[], _TArg extends string = ""> = | |
TArgs extends [infer TCurrent extends string, ...infer TNext extends string[]] | |
? _Flatten<TNext, `${_TArg}${TCurrent}`> | |
: _TArg; | |
export function stringConcat<const TArgs extends string[]>(...args: TArgs): StringConcat<TArgs> { | |
if (args.length === 0) return "" as StringConcat<TArgs>; | |
return "".concat(...args) as StringConcat<TArgs>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment