Skip to content

Commit

Permalink
Add TypeScript definition (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
UltiRequiem and sindresorhus authored Feb 2, 2022
1 parent b12cae3 commit aae7539
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
26 changes: 26 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
Returns an array filled with the specified input.
@param value - The value to fill the array with.
@param count - The number of items to fill the array with.
@example
```
import filledArray from 'filled-array';
filledArray('x', 3);
//=> ['x', 'x', 'x']
filledArray(0, 3);
//=> [0, 0, 0]
filledArray(index => {
return (++index % 3 ? '' : 'Fizz') + (index % 5 ? '' : 'Buzz') || index;
}, 15);
//=> [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz']
```
*/
export default function filledArray<T>(
value: T | ((index: number, count: number, currentArray: T[]) => T),
count: number
): T[];
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {expectType} from 'tsd';
import filledArray from './index.js';

expectType<string[]>(filledArray('x', 3));
expectType<number[]>(filledArray(0, 3));
expectType<string[]>(filledArray(index => `Hey ${index}`, 3));
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
"node": ">=12"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
],
"index.js",
"index.d.ts"
],
"keywords": [
"array",
"elements",
Expand All @@ -33,6 +34,7 @@
],
"devDependencies": {
"ava": "^4.0.0",
"tsd": "^0.19.1",
"xo": "^0.47.0"
}
}

0 comments on commit aae7539

Please sign in to comment.