Skip to content

Commit f790df7

Browse files
leon19sindresorhus
authored andcommitted
Add TypeScript definition (#20)
1 parent 25870cd commit f790df7

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

index.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export interface Options {
2+
/**
3+
* Use a key distance-based score to leniently accept typos of `yes` and `no`.
4+
*
5+
* @default false
6+
*/
7+
lenient?: boolean;
8+
9+
/**
10+
* Default value if no match was found.
11+
*
12+
* @default null
13+
*/
14+
default?: boolean | null;
15+
}
16+
17+
/**
18+
* Parse yes/no like values.
19+
* The following case-insensitive values are recognized: `'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0`
20+
*
21+
* @param input - Value that should be converted.
22+
* @returns The parsed input if it can be parsed or the default value defined in the `default` option.
23+
*/
24+
export default function yn(input: any, options?: Options): boolean | null;

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ module.exports = (val, opts) => {
2626

2727
return opts.default;
2828
};
29+
30+
module.exports.default = module.exports;

index.test-d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {expectType} from 'tsd-check';
2+
import yn from '.';
3+
4+
// Should use null as default when no options are given
5+
expectType<boolean | null>(yn('true'));
6+
7+
// Should use the default type when given
8+
expectType<boolean | null>(yn('true', {default: null}));
9+
10+
// Should use null as default when only the lenient option is given
11+
expectType<boolean | null>(yn('true', {lenient: true}));

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
"node": ">=4"
1818
},
1919
"scripts": {
20-
"test": "xo && ava"
20+
"test": "xo && ava && tsd-check"
2121
},
2222
"files": [
2323
"index.js",
24-
"lenient.js"
24+
"lenient.js",
25+
"index.d.ts"
2526
],
2627
"keywords": [
2728
"yn",
@@ -39,6 +40,7 @@
3940
],
4041
"devDependencies": {
4142
"ava": "*",
43+
"tsd-check": "*",
4244
"xo": "*"
4345
}
4446
}

0 commit comments

Comments
 (0)