diff --git a/split.ts b/split.ts new file mode 100644 index 0000000..be97c60 --- /dev/null +++ b/split.ts @@ -0,0 +1,31 @@ +import type { Equal, Expect } from '@type-challenges/utils' + +type cases = [ + Expect, ['Hi! How are you?']>>, + Expect, ['Hi!', 'How', 'are', 'you?']>>, + Expect, ['H', 'i', '!', ' ', 'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u', '?']>>, + Expect, []>>, + Expect, ['']>>, + Expect, string[]>>, + Expect, string[]>>, + Expect, string[]>>, +] + +type X = Split<'This is some real shit', string>; + +type Split< + S extends string, + Z extends string, +> = +S extends string + ? string extends S + ? string[] + : Z extends string + ? string extends Z + ? string[] + : S extends `${infer Head}${Z}${infer Tail}` + ? [Head, ...Split] + : Z extends '' ? [] : [S] + : never + : never +;