Skip to content

Instantly share code, notes, and snippets.

@aparx
Last active February 17, 2024 21:16
Show Gist options
  • Save aparx/b24c68ffbf1c7b52f15c8ce66b73f7d9 to your computer and use it in GitHub Desktop.
Save aparx/b24c68ffbf1c7b52f15c8ce66b73f7d9 to your computer and use it in GitHub Desktop.
Typesafe string concat
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