Created
February 6, 2025 09:33
-
-
Save AlexGeb/98590bc17f15e3a9f4ce7b141f3a96f9 to your computer and use it in GitHub Desktop.
split.ts
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
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