-
-
Notifications
You must be signed in to change notification settings - Fork 571
/
Copy pathstring-repeat.d.ts
47 lines (39 loc) · 1.09 KB
/
string-repeat.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
36
37
38
39
40
41
42
43
44
45
46
47
import type {IsNumericLiteral} from './is-literal';
import type {IsNegative} from './numeric';
/**
Returns a new string which contains the specified number of copies of a given string, just like `String#repeat()`.
@example
```
import {StringRepeat} from 'type-fest';
declare function stringRepeat<
Input extends string,
Count extends number
>(input: Input, count: Count): StringRepeat<Input, Count>;
// The return type is the exact string literal, not just `string`.
stringRepeat('foo', 2);
//=> 'foofoo'
stringRepeat('=', 3);
//=> '==='
```
@category String
@category Template literal
*/
export type StringRepeat<
Input extends string,
Count extends number,
> = StringRepeatHelper<Input, Count>;
type StringRepeatHelper<
Input extends string,
Count extends number,
Counter extends never[] = [],
Accumulator extends string = '',
> =
IsNegative<Count> extends true
? never
: Input extends ''
? ''
: Count extends Counter['length']
? Accumulator
: IsNumericLiteral<Count> extends false
? string
: StringRepeatHelper<Input, Count, [...Counter, never], `${Accumulator}${Input}`>;