From 2347afcc47ef0d39d221b9ee404ff16adebefccc Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Mon, 25 Feb 2019 18:23:10 +0100 Subject: [PATCH] [onetime] Update types to v3.0 --- types/onetime/index.d.ts | 45 ++++++++++++++++++++++++++++++++++ types/onetime/onetime-tests.ts | 12 +++++++++ types/onetime/tsconfig.json | 23 +++++++++++++++++ types/onetime/tslint.json | 1 + 4 files changed, 81 insertions(+) create mode 100644 types/onetime/index.d.ts create mode 100644 types/onetime/onetime-tests.ts create mode 100644 types/onetime/tsconfig.json create mode 100644 types/onetime/tslint.json diff --git a/types/onetime/index.d.ts b/types/onetime/index.d.ts new file mode 100644 index 0000000000..f1c6b56ad0 --- /dev/null +++ b/types/onetime/index.d.ts @@ -0,0 +1,45 @@ +// Type definitions for onetime 3.0 +// Project: https://github.com/sindresorhus/onetime#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 + +export = oneTime; + +/** + * Ensure a function is only called once. When called multiple times it will return the return value from the first call. + * + * @param fn Function that should only be called once. + * @returns A function that only calls `fn` once. + */ +declare function oneTime( + fn: (...args: T) => R, + options?: oneTime.Options +): (...args: T) => R; + +declare namespace oneTime { + /** + * Get the number of times `fn` has been called. + * + * @param fn Function to get call count from. + * @returns A number representing how many times `fn` has been called. + * + * @example + * const foo = onetime(() => {}); + * foo(); + * foo(); + * foo(); + * + * console.log(onetime.callCount(foo)); + * //=> 3 + */ + function callCount(fn: (...args: any[]) => any): number | undefined; + + interface Options { + /** + * Throw an error when called more than once. + * @default false + */ + throw?: boolean; + } +} diff --git a/types/onetime/onetime-tests.ts b/types/onetime/onetime-tests.ts new file mode 100644 index 0000000000..4678eb45db --- /dev/null +++ b/types/onetime/onetime-tests.ts @@ -0,0 +1,12 @@ +import onetime = require('onetime'); + +const foo = onetime(() => 5); +foo(); // $ExpectType number + +const foo2 = onetime(() => true, { throw: true }); +foo2(); // $ExpectType boolean + +onetime((t1: boolean) => 5)(true); // $ExpectType number +onetime((t1: boolean, t2: string) => 5)(true, ''); // $ExpectType number + +onetime.callCount((t1: boolean, t2: string) => 5); // $ExpectType number | undefined diff --git a/types/onetime/tsconfig.json b/types/onetime/tsconfig.json new file mode 100644 index 0000000000..39df78e5bc --- /dev/null +++ b/types/onetime/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "onetime-tests.ts" + ] +} diff --git a/types/onetime/tslint.json b/types/onetime/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/onetime/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }