diff --git a/types/wyt/index.d.ts b/types/wyt/index.d.ts new file mode 100644 index 0000000000..ff798d4380 --- /dev/null +++ b/types/wyt/index.d.ts @@ -0,0 +1,33 @@ +// Type definitions for wyt 1.3 +// Project: https://github.com/maxkueng/wyt +// Definitions by: Emily Marigold Klassen +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +export = wyt; + +/** + * @param turnsPerInterval The number of turns that can be taken within a + * certain interval. + * @param interval The interval within which `turnsPerInterval` can be executed. + * + * @returns a function `(turns?: number) => Promise` that can be called + * before before requesting a rate-limited resource (i.e. wait for its turn) in + * order to not exceed the limit. + */ +declare function wyt(turnsPerInterval: number, interval: number): wyt.WaitTurn; + +declare namespace wyt { + /** + * @param turns (default=1) The number of turns that will be taken at the + * same time. You can not await more turns at the same time as + * `turnsPerInterval`. + * + * @returns a promise `Promise` that will resolve with the time + * waited as soon as another turn can be taken. If more than + * `turnsPerInterval` are requested at the same time the promise will + * reject. + */ + + type WaitTurn = (turns?: number) => Promise; +} diff --git a/types/wyt/tsconfig.json b/types/wyt/tsconfig.json new file mode 100644 index 0000000000..bb9ad36060 --- /dev/null +++ b/types/wyt/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "wyt-tests.ts" + ] +} diff --git a/types/wyt/tslint.json b/types/wyt/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/wyt/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/wyt/wyt-tests.ts b/types/wyt/wyt-tests.ts new file mode 100644 index 0000000000..773c9e8f56 --- /dev/null +++ b/types/wyt/wyt-tests.ts @@ -0,0 +1,21 @@ +import wyt = require('wyt'); + +// Extracted from wyt's readme. + +(async () => { +/** + * The ticket desk can handle 2 customers per 1000 milliseconds. + */ +const waitTurn = wyt(2, 1000); + +// $ExpectType number +await waitTurn(); + +// $ExpectType number +await waitTurn(2); + +// $ExpectType number +await waitTurn(3); +// UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id:2): +// Error: Turns can not be greater than the number of turns per interval +})();