UNPKG

3.79 kBTypeScriptView Raw
1declare module 'timers' {
2 import { Abortable } from 'events';
3 import { setTimeout as setTimeoutPromise, setImmediate as setImmediatePromise, setInterval as setIntervalPromise } from 'timers/promises';
4
5 interface TimerOptions extends Abortable {
6 /**
7 * Set to `false` to indicate that the scheduled `Timeout`
8 * should not require the Node.js event loop to remain active.
9 * @default true
10 */
11 ref?: boolean | undefined;
12 }
13
14 let setTimeout: typeof global.setTimeout;
15 let clearTimeout: typeof global.clearTimeout;
16 let setInterval: typeof global.setInterval;
17 let clearInterval: typeof global.clearInterval;
18 let setImmediate: typeof global.setImmediate;
19 let clearImmediate: typeof global.clearImmediate;
20
21 global {
22 namespace NodeJS {
23 // compatibility with older typings
24 interface Timer extends RefCounted {
25 hasRef(): boolean;
26 refresh(): this;
27 [Symbol.toPrimitive](): number;
28 }
29
30 interface Immediate extends RefCounted {
31 hasRef(): boolean;
32 _onImmediate: Function; // to distinguish it from the Timeout class
33 }
34
35 interface Timeout extends Timer {
36 hasRef(): boolean;
37 refresh(): this;
38 [Symbol.toPrimitive](): number;
39 }
40 }
41
42 function setTimeout<TArgs extends any[]>(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs): NodeJS.Timeout;
43 // util.promisify no rest args compability
44 // tslint:disable-next-line void-return
45 function setTimeout(callback: (args: void) => void): NodeJS.Timeout;
46 namespace setTimeout {
47 const __promisify__: typeof setTimeoutPromise;
48 }
49 function clearTimeout(timeoutId: NodeJS.Timeout): void;
50
51 function setInterval<TArgs extends any[]>(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs): NodeJS.Timer;
52 // util.promisify no rest args compability
53 // tslint:disable-next-line void-return
54 function setInterval(callback: (args: void) => void, ms?: number): NodeJS.Timer;
55 namespace setInterval {
56 const __promisify__: typeof setIntervalPromise;
57 }
58 function clearInterval(intervalId: NodeJS.Timeout): void;
59
60 function setImmediate<TArgs extends any[]>(callback: (...args: TArgs) => void, ...args: TArgs): NodeJS.Immediate;
61 // util.promisify no rest args compability
62 // tslint:disable-next-line void-return
63 function setImmediate(callback: (args: void) => void): NodeJS.Immediate;
64 namespace setImmediate {
65 const __promisify__: typeof setImmediatePromise;
66 }
67 function clearImmediate(immediateId: NodeJS.Immediate): void;
68
69 function queueMicrotask(callback: () => void): void;
70 }
71}
72
73declare module 'node:timers' {
74 export * from 'timers';
75}
76
77declare module 'timers/promises' {
78 import { TimerOptions } from 'timers';
79
80 /**
81 * Returns a promise that resolves after the specified delay in milliseconds.
82 * @param delay defaults to 1
83 */
84 function setTimeout<T = void>(delay?: number, value?: T, options?: TimerOptions): Promise<T>;
85
86 /**
87 * Returns a promise that resolves in the next tick.
88 */
89 function setImmediate<T = void>(value?: T, options?: TimerOptions): Promise<T>;
90
91 /**
92 *
93 * Returns an async iterator that generates values in an interval of delay ms.
94 * @param delay defaults to 1
95 */
96 function setInterval<T = void>(delay?: number, value?: T, options?: TimerOptions): AsyncIterable<T>;
97}
98
99declare module 'node:timers/promises' {
100 export * from 'timers/promises';
101}