diff --git a/types/safe-timers/index.d.ts b/types/safe-timers/index.d.ts new file mode 100644 index 0000000000..14dd3c823f --- /dev/null +++ b/types/safe-timers/index.d.ts @@ -0,0 +1,49 @@ +// Type definitions for safe-timers 1.1 +// Project: https://github.com/Wizcorp/safe-timers#readme +// Definitions by: Kamil Socha +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 + +export const maxInterval: number; + +export class Timeout { + /** + * Cancels timeout. + */ + clear(): void; +} + +/** + * Creates and returns a `Timeout` instance that will call `callback` after at least `interval` milliseconds have passed. + * All arguments passed after the `interval` will be passed to the `callback` once it gets invoked. + */ +export function setTimeout(callback: (...args: T) => void, interval: number, ...args: T): Timeout; + +/** + * Creates and returns a `Timeout` instance that will call `callback` when our clock reaches the given `timestamp` (in milliseconds). + * All arguments passed after the `timestamp` will be passed to the `callback` once it gets invoked. + */ +export function setTimeoutAt(callback: (...args: T) => void, timestamp: number, ...args: T): Timeout; + +/** + * Cancels `timeout`. + */ +export function clearTimeout(timeout: Timeout): void; + +export class Interval { + /** + * Cancels interval. + */ + clear(): void; +} + +/** + * Creates and returns an `Interval` instance that will call `callback` after at least every `interval` milliseconds. + * All arguments passed after the `interval` will be passed to the `callback` when it gets invoked. + */ +export function setInterval(callback: (...args: T) => void, interval: number, ...args: T): Interval; + +/** + * Cancels `interval`. + */ +export function clearInterval(interval: Interval): void; diff --git a/types/safe-timers/safe-timers-tests.ts b/types/safe-timers/safe-timers-tests.ts new file mode 100644 index 0000000000..8cac01402a --- /dev/null +++ b/types/safe-timers/safe-timers-tests.ts @@ -0,0 +1,29 @@ +import { + maxInterval, + Timeout, + setTimeout, + setTimeoutAt, + clearTimeout, + Interval, + setInterval, + clearInterval, +} from 'safe-timers'; + +const maxIntervalTest: number = maxInterval; + +const timeout: Timeout = setTimeout(() => {}, 1000); +const timeoutWithArgs: Timeout = setTimeout((hero: string, powerLevel: number) => {}, 1000, 'Goku', 9000); + +const timeoutAt: Timeout = setTimeoutAt(() => {}, 1000); +const timeoutAtWithArgs: Timeout = setTimeoutAt((hero: string, powerLevel: number) => {}, 1000, 'Goku', 9000); + +timeout.clear(); + +clearTimeout(timeout); + +const interval: Interval = setInterval(() => {}, 1000); +const intervalWithArgs: Interval = setInterval((hero: string, powerLevel: number) => {}, 1000, 'Goku', 9000); + +interval.clear(); + +clearInterval(interval); diff --git a/types/safe-timers/tsconfig.json b/types/safe-timers/tsconfig.json new file mode 100644 index 0000000000..556b7a6ae1 --- /dev/null +++ b/types/safe-timers/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", + "safe-timers-tests.ts" + ] +} diff --git a/types/safe-timers/tslint.json b/types/safe-timers/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/safe-timers/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }