[onetime] Update types to v3.0

This commit is contained in:
Dimitri Benin 2019-02-25 18:23:10 +01:00
parent c5c72bcb9b
commit 2347afcc47
4 changed files with 81 additions and 0 deletions

45
types/onetime/index.d.ts vendored Normal file
View File

@ -0,0 +1,45 @@
// Type definitions for onetime 3.0
// Project: https://github.com/sindresorhus/onetime#readme
// Definitions by: BendingBender <https://github.com/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<T extends any[], R>(
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;
}
}

View File

@ -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

View File

@ -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"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }