Skip to content

Instantly share code, notes, and snippets.

@AlexGeb
Created February 6, 2025 09:33
Show Gist options
  • Save AlexGeb/98590bc17f15e3a9f4ce7b141f3a96f9 to your computer and use it in GitHub Desktop.
Save AlexGeb/98590bc17f15e3a9f4ce7b141f3a96f9 to your computer and use it in GitHub Desktop.
split.ts
type Split<
V extends string,
S extends string,
> = V extends `${infer Head}${S}${infer Rest}`
? [Head, ...Split<Rest, S>]
: Array<V>;
const split = <V extends string, S extends string>(
value: V,
separator: S,
): Split<V, S> => String.split(value, separator) as any;
type V1 = 'foo' | 'bar' | 'baz';
type V2 = 'foa' | 'bor' | 'boz';
type V = `${V1}-${V2}`;
const value: V = `foo-foa`;
const [a, b] = split(value, '-');
const r2 = split('foo', '-');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment