forked from sindresorhus/type-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-slice.d.ts
More file actions
37 lines (29 loc) · 728 Bytes
/
Copy pathstring-slice.d.ts
File metadata and controls
37 lines (29 loc) · 728 Bytes
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
36
37
import type {Join} from './join';
import type {ArraySlice} from './array-slice';
import type {StringToArray} from './internal';
/**
Returns a string slice of a given range, just like `String#slice()`.
@see {ArraySlice}
@example
```
import type {StringSlice} from 'type-fest';
StringSlice<'abcde', 0, 2>;
//=> 'ab'
StringSlice<'abcde', 1>;
//=> 'bcde'
StringSlice<'abcde', 0, -1>;
//=> 'abcd'
StringSlice<'abcde', -2, -1>;
//=> 'd'
```
@category String
*/
export type StringSlice<
S extends string,
Start extends number = 0,
End extends number = StringToArray<S>['length'],
> = string extends S
? string
: ArraySlice<StringToArray<S>, Start, End> extends infer R extends readonly string[]
? Join<R, ''>
: never;