[wyt] Add typings (#43408)

This commit is contained in:
Emily Marigold Klassen 2020-03-26 22:17:43 -07:00 committed by GitHub
parent 2cb4829c31
commit 7621f3b99c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 0 deletions

33
types/wyt/index.d.ts vendored Normal file
View File

@ -0,0 +1,33 @@
// Type definitions for wyt 1.3
// Project: https://github.com/maxkueng/wyt
// Definitions by: Emily Marigold Klassen <https://github.com/forivall>
// 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<number>` 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<number>` 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<number>;
}

23
types/wyt/tsconfig.json Normal file
View File

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

1
types/wyt/tslint.json Normal file
View File

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

21
types/wyt/wyt-tests.ts Normal file
View File

@ -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
})();