diff --git a/index.d.ts b/index.d.ts index 6b7aaac..c72a414 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,24 +1,65 @@ -export interface Options { - /** - * Use a key distance-based score to leniently accept typos of `yes` and `no`. - * - * @default false - */ - lenient?: boolean; +declare namespace yn { + interface Options { + /** + Use a key distance-based score to leniently accept typos of `yes` and `no`. - /** - * Default value if no match was found. - * - * @default null - */ - default?: boolean | null; + @default false + */ + readonly lenient?: boolean; + + /** + Default value if no match was found. + + @default null + */ + readonly default?: boolean | null; + } + + interface OptionsWithDefault extends Options { + default: boolean; + } } -/** - * Parse yes/no like values. - * The following case-insensitive values are recognized: `'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0` - * - * @param input - Value that should be converted. - * @returns The parsed input if it can be parsed or the default value defined in the `default` option. - */ -export default function yn(input: any, options?: Options): boolean | null; +declare const yn: { + /** + Parse yes/no like values. + + The following case-insensitive values are recognized: `'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0` + + @param input - Value that should be converted. + @returns The parsed input if it can be parsed or the default value defined in the `default` option. + + @example + ``` + import yn = require('yn'); + + yn('y'); + //=> true + + yn('NO'); + //=> false + + yn(true); + //=> true + + yn('abomasum'); + //=> null + + yn('abomasum', {default: false}); + //=> false + + yn('mo', {lenient: true}); + //=> false + ``` + */ + (input: unknown, options: yn.OptionsWithDefault): boolean; + (input: unknown, options?: yn.Options): boolean | null; + + // TODO: Remove this for the next major release, refactor the whole definition to: + // declare function yn(input: unknown, options: yn.OptionsWithDefault): boolean; + // declare function yn(input: unknown, options?: yn.Options): boolean | null; + // export = yn; + default: typeof yn; +}; + +export = yn; diff --git a/index.js b/index.js index fd91d63..a5a24c1 100644 --- a/index.js +++ b/index.js @@ -29,4 +29,5 @@ const yn = (input, options) => { }; module.exports = yn; +// TODO: Remove this for the next major release module.exports.default = yn; diff --git a/index.test-d.ts b/index.test-d.ts index 339ef3b..220701d 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,11 +1,6 @@ -import {expectType} from 'tsd-check'; -import yn from '.'; +import {expectType} from 'tsd'; +import yn = require('.'); -// Should use null as default when no options are given -expectType(yn('true')); - -// Should use the default type when given -expectType(yn('true', {default: null})); - -// Should use null as default when only the lenient option is given -expectType(yn('true', {lenient: true})); +expectType(yn('y')); +expectType(yn('mo', {lenient: true})); +expectType(yn('abomasum', {default: false})); diff --git a/package.json b/package.json index 933384e..b08b4d6 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "node": ">=6" }, "scripts": { - "test": "xo && ava && tsd-check" + "test": "xo && ava && tsd" }, "files": [ "index.js", @@ -35,8 +35,8 @@ "lenient" ], "devDependencies": { - "ava": "^0.25.0", - "tsd-check": "^0.2.1", - "xo": "^0.23.0" + "ava": "^1.4.1", + "tsd": "^0.7.2", + "xo": "^0.24.0" } }