-
-
Notifications
You must be signed in to change notification settings - Fork 572
/
Copy pathsplit.d.ts
35 lines (28 loc) · 866 Bytes
/
split.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
Represents an array of strings split using a given character or character set.
Use-case: Defining the return type of a method like `String.prototype.split`.
@example
```
import type {Split} from 'type-fest';
declare function split<S extends string, D extends string>(string: S, separator: D): Split<S, D>;
type Item = 'foo' | 'bar' | 'baz' | 'waldo';
const items = 'foo,bar,baz,waldo';
let array: Item[];
array = split(items, ',');
```
@category String
@category Template literal
*/
export type Split<
S extends string,
Delimiter extends string,
> = SplitHelper<S, Delimiter>;
type SplitHelper<
S extends string,
Delimiter extends string,
Accumulator extends string[] = [],
> = S extends `${infer Head}${Delimiter}${infer Tail}`
? SplitHelper<Tail, Delimiter, [...Accumulator, Head]>
: Delimiter extends ''
? Accumulator
: [...Accumulator, S];