UNPKG

689 BTypeScriptView Raw
1/**
2Represents an array of strings split using a given character or character set.
3
4Use-case: Defining the return type of a method like `String.prototype.split`.
5
6@example
7```
8import type {Split} from 'type-fest';
9
10declare function split<S extends string, D extends string>(string: S, separator: D): Split<S, D>;
11
12type Item = 'foo' | 'bar' | 'baz' | 'waldo';
13const items = 'foo,bar,baz,waldo';
14let array: Item[];
15
16array = split(items, ',');
17```
18
19@category String
20@category Template literal
21*/
22export type Split<
23 S extends string,
24 Delimiter extends string,
25> = S extends `${infer Head}${Delimiter}${infer Tail}`
26 ? [Head, ...Split<Tail, Delimiter>]
27 : S extends Delimiter
28 ? []
29 : [S];