Skip to content

Commit 6fb9f33

Browse files
BendingBendersindresorhus
authored andcommitted
Refactor TypeScript definition to CommonJS compatible export (#23)
1 parent 723245e commit 6fb9f33

File tree

4 files changed

+72
-35
lines changed

4 files changed

+72
-35
lines changed

index.d.ts

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,65 @@
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;
1+
declare namespace yn {
2+
interface Options {
3+
/**
4+
Use a key distance-based score to leniently accept typos of `yes` and `no`.
85
9-
/**
10-
* Default value if no match was found.
11-
*
12-
* @default null
13-
*/
14-
default?: boolean | null;
6+
@default false
7+
*/
8+
readonly lenient?: boolean;
9+
10+
/**
11+
Default value if no match was found.
12+
13+
@default null
14+
*/
15+
readonly default?: boolean | null;
16+
}
17+
18+
interface OptionsWithDefault extends Options {
19+
default: boolean;
20+
}
1521
}
1622

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;
23+
declare const yn: {
24+
/**
25+
Parse yes/no like values.
26+
27+
The following case-insensitive values are recognized: `'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0`
28+
29+
@param input - Value that should be converted.
30+
@returns The parsed input if it can be parsed or the default value defined in the `default` option.
31+
32+
@example
33+
```
34+
import yn = require('yn');
35+
36+
yn('y');
37+
//=> true
38+
39+
yn('NO');
40+
//=> false
41+
42+
yn(true);
43+
//=> true
44+
45+
yn('abomasum');
46+
//=> null
47+
48+
yn('abomasum', {default: false});
49+
//=> false
50+
51+
yn('mo', {lenient: true});
52+
//=> false
53+
```
54+
*/
55+
(input: unknown, options: yn.OptionsWithDefault): boolean;
56+
(input: unknown, options?: yn.Options): boolean | null;
57+
58+
// TODO: Remove this for the next major release, refactor the whole definition to:
59+
// declare function yn(input: unknown, options: yn.OptionsWithDefault): boolean;
60+
// declare function yn(input: unknown, options?: yn.Options): boolean | null;
61+
// export = yn;
62+
default: typeof yn;
63+
};
64+
65+
export = yn;

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ const yn = (input, options) => {
2929
};
3030

3131
module.exports = yn;
32+
// TODO: Remove this for the next major release
3233
module.exports.default = yn;

index.test-d.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import {expectType} from 'tsd-check';
2-
import yn from '.';
1+
import {expectType} from 'tsd';
2+
import yn = require('.');
33

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}));
4+
expectType<boolean | null>(yn('y'));
5+
expectType<boolean | null>(yn('mo', {lenient: true}));
6+
expectType<boolean>(yn('abomasum', {default: false}));

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"node": ">=6"
1414
},
1515
"scripts": {
16-
"test": "xo && ava && tsd-check"
16+
"test": "xo && ava && tsd"
1717
},
1818
"files": [
1919
"index.js",
@@ -35,8 +35,8 @@
3535
"lenient"
3636
],
3737
"devDependencies": {
38-
"ava": "^0.25.0",
39-
"tsd-check": "^0.2.1",
40-
"xo": "^0.23.0"
38+
"ava": "^1.4.1",
39+
"tsd": "^0.7.2",
40+
"xo": "^0.24.0"
4141
}
4242
}

0 commit comments

Comments
 (0)