Skip to content

Commit aae7539

Browse files
Add TypeScript definition (#7)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent b12cae3 commit aae7539

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

index.d.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
Returns an array filled with the specified input.
3+
4+
@param value - The value to fill the array with.
5+
@param count - The number of items to fill the array with.
6+
7+
@example
8+
```
9+
import filledArray from 'filled-array';
10+
11+
filledArray('x', 3);
12+
//=> ['x', 'x', 'x']
13+
14+
filledArray(0, 3);
15+
//=> [0, 0, 0]
16+
17+
filledArray(index => {
18+
return (++index % 3 ? '' : 'Fizz') + (index % 5 ? '' : 'Buzz') || index;
19+
}, 15);
20+
//=> [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz']
21+
```
22+
*/
23+
export default function filledArray<T>(
24+
value: T | ((index: number, count: number, currentArray: T[]) => T),
25+
count: number
26+
): T[];

index.test-d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {expectType} from 'tsd';
2+
import filledArray from './index.js';
3+
4+
expectType<string[]>(filledArray('x', 3));
5+
expectType<number[]>(filledArray(0, 3));
6+
expectType<string[]>(filledArray(index => `Hey ${index}`, 3));

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
"node": ">=12"
1717
},
1818
"scripts": {
19-
"test": "xo && ava"
19+
"test": "xo && ava && tsd"
2020
},
2121
"files": [
22-
"index.js"
23-
],
22+
"index.js",
23+
"index.d.ts"
24+
],
2425
"keywords": [
2526
"array",
2627
"elements",
@@ -33,6 +34,7 @@
3334
],
3435
"devDependencies": {
3536
"ava": "^4.0.0",
37+
"tsd": "^0.19.1",
3638
"xo": "^0.47.0"
3739
}
3840
}

0 commit comments

Comments
 (0)