|
1 | 1 | import * as timers from "node:timers"; |
2 | 2 | import { promisify } from "node:util"; |
3 | | -{ |
4 | | - { |
5 | | - const immediate = timers |
6 | | - .setImmediate(() => { |
7 | | - console.log("immediate"); |
8 | | - }) |
9 | | - .unref() |
10 | | - .ref(); |
11 | | - const b: boolean = immediate.hasRef(); |
12 | | - timers.clearImmediate(immediate); |
13 | | - } |
14 | | - { |
15 | | - const timeout = timers |
16 | | - .setInterval(() => { |
17 | | - console.log("interval"); |
18 | | - }, 20) |
19 | | - .unref() |
20 | | - .ref() |
21 | | - .refresh(); |
22 | | - const b: boolean = timeout.hasRef(); |
23 | | - timers.clearInterval(timeout); |
24 | | - timers.clearInterval(timeout[Symbol.toPrimitive]()); |
25 | | - } |
26 | | - { |
27 | | - const timeout = timers |
28 | | - .setTimeout(() => { |
29 | | - console.log("timeout"); |
30 | | - }, 20) |
31 | | - .unref() |
32 | | - .ref() |
33 | | - .refresh(); |
34 | | - const b: boolean = timeout.hasRef(); |
35 | | - timers.clearTimeout(timeout); |
36 | | - timers.clearTimeout(timeout[Symbol.toPrimitive]()); |
37 | | - } |
38 | | - async function testPromisify(doSomething: { |
39 | | - (foo: any, onSuccessCallback: (result: string) => void, onErrorCallback: (reason: any) => void): void; |
40 | | - [promisify.custom](foo: any): Promise<string>; |
41 | | - }) { |
42 | | - const setTimeout = promisify(timers.setTimeout); |
43 | | - // eslint-disable-next-line @typescript-eslint/no-invalid-void-type |
44 | | - let v: void = await setTimeout(100); |
45 | | - let s: string = await setTimeout(100, ""); |
46 | 3 |
|
47 | | - const setImmediate = promisify(timers.setImmediate); |
48 | | - v = await setImmediate(); |
49 | | - s = await setImmediate(""); |
| 4 | +{ |
| 5 | + const promises: typeof import("node:timers/promises") = timers.promises; |
| 6 | +} |
50 | 7 |
|
51 | | - // $ExpectType (foo: any) => Promise<string> |
52 | | - const doSomethingPromise = promisify(doSomething); |
| 8 | +{ |
| 9 | + // $ExpectType Immediate |
| 10 | + const immediate = timers.setImmediate(() => {}); |
| 11 | + // $ExpectType boolean |
| 12 | + immediate.hasRef(); |
| 13 | + // $ExpectType Immediate |
| 14 | + immediate.ref(); |
| 15 | + // $ExpectType Immediate |
| 16 | + immediate.unref(); |
53 | 17 |
|
54 | | - // $ExpectType string |
55 | | - s = await doSomethingPromise("foo"); |
56 | | - } |
| 18 | + timers.clearImmediate(immediate); |
| 19 | + immediate[Symbol.dispose](); |
57 | 20 | } |
58 | 21 |
|
59 | 22 | { |
60 | | - const setTimeout = promisify(timers.setTimeout); |
| 23 | + // $ExpectType Timeout |
| 24 | + const interval = timers.setInterval(() => {}, 100); |
| 25 | + // $ExpectType Timeout |
| 26 | + interval.close(); |
| 27 | + // $ExpectType boolean |
| 28 | + interval.hasRef(); |
| 29 | + // $ExpectType Timeout |
| 30 | + interval.ref(); |
| 31 | + // $ExpectType Timeout |
| 32 | + interval.refresh(); |
| 33 | + // $ExpectType Timeout |
| 34 | + interval.unref(); |
61 | 35 |
|
62 | | - const ac = new AbortController(); |
63 | | - |
64 | | - const signal = ac.signal; |
65 | | - setTimeout(10, undefined, { signal }); |
66 | | - ac.abort(); |
| 36 | + timers.clearInterval(interval); |
| 37 | + timers.clearInterval(interval[Symbol.toPrimitive]()); |
| 38 | + interval[Symbol.dispose](); |
67 | 39 | } |
68 | 40 |
|
69 | 41 | { |
70 | | - const setImmediate = promisify(timers.setImmediate); |
| 42 | + // $ExpectType Timeout |
| 43 | + const timeout = timers.setTimeout(() => {}, 100); |
| 44 | + // $ExpectType Timeout |
| 45 | + timeout.close(); |
| 46 | + // $ExpectType boolean |
| 47 | + timeout.hasRef(); |
| 48 | + // $ExpectType Timeout |
| 49 | + timeout.ref(); |
| 50 | + // $ExpectType Timeout |
| 51 | + timeout.refresh(); |
| 52 | + // $ExpectType Timeout |
| 53 | + timeout.unref(); |
71 | 54 |
|
72 | | - const ac = new AbortController(); |
| 55 | + timers.clearTimeout(timeout); |
| 56 | + timers.clearTimeout(timeout[Symbol.toPrimitive]()); |
| 57 | + timeout[Symbol.dispose](); |
| 58 | +} |
73 | 59 |
|
74 | | - const signal = ac.signal; |
75 | | - setImmediate(10, { signal }); |
76 | | - ac.abort(); |
| 60 | +// Test custom promisifiers |
| 61 | +{ |
| 62 | + const setImmediate: typeof timers.promises.setImmediate = promisify(timers.setImmediate); |
| 63 | + const setTimeout: typeof timers.promises.setTimeout = promisify(timers.setTimeout); |
| 64 | + // @ts-expect-error setInterval is not promisifiable |
| 65 | + const setInterval: typeof timers.promises.setInterval = promisify(timers.setInterval); |
77 | 66 | } |
78 | 67 |
|
79 | | -// unresolved callback argument types |
| 68 | +// Allow single callback parameter of type `unknown` to be omitted from passed arguments |
80 | 69 | { |
81 | 70 | // `NodeJS.*` is present to make sure we're not using `dom` types |
82 | 71 | new Promise((resolve): NodeJS.Timeout => timers.setTimeout(resolve, 100)); |
83 | | - new Promise((resolve): NodeJS.Timer => timers.setInterval(resolve, 100)); |
84 | | - // tslint:disable-next-line no-unnecessary-callback-wrapper |
| 72 | + new Promise((resolve): NodeJS.Timeout => timers.setInterval(resolve, 100)); |
85 | 73 | new Promise((resolve): NodeJS.Immediate => timers.setImmediate(resolve)); |
| 74 | + // @ts-expect-error single argument should not be optional if not of type `unknown` |
| 75 | + const timeout: NodeJS.Timeout = timers.setTimeout((s: string) => {}, 100); |
86 | 76 | } |
87 | 77 |
|
88 | 78 | // globals |
89 | 79 | { |
90 | | - setTimeout((a: number, b: string) => {}, 12, 1, "test"); |
91 | | - setInterval((a: number, b: string) => {}, 12, 1, "test"); |
92 | | - setImmediate((a: number, b: string) => {}, 1, "test"); |
93 | | - queueMicrotask(() => { |
94 | | - // cool |
95 | | - }); |
| 80 | + const setImmediate: typeof timers.setImmediate = globalThis.setImmediate; |
| 81 | + const setInterval: typeof timers.setInterval = globalThis.setInterval; |
| 82 | + const setTimeout: typeof timers.setTimeout = globalThis.setTimeout; |
| 83 | + const clearImmediate: typeof timers.clearImmediate = globalThis.clearImmediate; |
| 84 | + const clearInterval: typeof timers.clearInterval = globalThis.clearInterval; |
| 85 | + const clearTimeout: typeof timers.clearTimeout = globalThis.clearTimeout; |
96 | 86 |
|
97 | | - function waitFor(options?: { timeout: number }) { |
98 | | - const timerId = options && setTimeout(() => {}, options.timeout); |
99 | | - clearTimeout(timerId); |
100 | | - timerId?.[Symbol.dispose](); |
101 | | - const intervalId = options && setTimeout(() => {}, options.timeout); |
102 | | - clearInterval(intervalId); |
103 | | - intervalId?.[Symbol.dispose](); |
104 | | - const immediateId = options && setImmediate(() => {}); |
105 | | - clearImmediate(immediateId); |
106 | | - immediateId?.[Symbol.dispose](); |
107 | | - } |
| 87 | + queueMicrotask(() => {}); |
108 | 88 | } |
0 commit comments