feat: Add setImmediate (#40012)

This commit is contained in:
ExE Boss
2019-11-01 16:22:23 -07:00
committed by Jesse Trinity
parent 92a0fe0a28
commit 010944299e
9 changed files with 159 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
// Type definitions for setimmediate 1.0
// Project: https://github.com/yuzujs/setImmediate#readme
// Definitions by: ExE Boss <https://github.com/ExE-Boss>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/**
* Schedules a macrotask to run after the current events have been processed.
*
* Unlike microtasks (scheduled using the Node 0.10+ `process.nextTick` API),
* where scheduling additional microtasks inside a microtask will cause them
* to be run inside the same microtask checkpoint, any macrotasks scheduled
* inside a macrotask will not be executed until the next iteration
* of the event loop.
*
* @param callback The macrotask to schedule.
* @param args The arguments to pass to the macrotask callback.
*
* @return The ID of the macrotask, which can be used to abort
* the macrotask with `clearImmediate`.
*/
declare function setImmediate(callback: (...args: any[]) => void, ...args: any[]): number;
/**
* Aborts the specified macrotask before it's run.
*
* @param handle The ID of the macrotask to remove from the macrotask queue.
*/
declare function clearImmediate(handle: number): void;
+11
View File
@@ -0,0 +1,11 @@
{
"private": true,
"types": "index",
"typesVersions": {
">=3.1.0-0": {
"*": [
"ts3.1/*"
]
}
}
}
+21
View File
@@ -0,0 +1,21 @@
import './index';
// $ExpectType number
const i = setImmediate((...args) => {
args; // $ExpectType any[]
});
clearImmediate(i);
// Errors:
setImmediate(); // $ExpectError
clearImmediate(); // $ExpectError
clearImmediate(null); // $ExpectError
clearImmediate(undefined); // $ExpectError
clearImmediate('string'); // $ExpectError
clearImmediate(Symbol.iterator); // $ExpectError
clearImmediate(true); // $ExpectError
clearImmediate(false); // $ExpectError
clearImmediate({}); // $ExpectError
clearImmediate(Function); // $ExpectError
+23
View File
@@ -0,0 +1,23 @@
/**
* Schedules a macrotask to run after the current events have been processed.
*
* Unlike microtasks (scheduled using the Node 0.10+ `process.nextTick` API),
* where scheduling additional microtasks inside a microtask will cause them
* to be run inside the same microtask checkpoint, any macrotasks scheduled
* inside a macrotask will not be executed until the next iteration
* of the event loop.
*
* @param callback The macrotask to schedule.
* @param args The arguments to pass to the macrotask callback.
*
* @return The ID of the macrotask, which can be used to abort
* the macrotask with `clearImmediate`.
*/
declare function setImmediate<T extends unknown[]>(callback: (...args: T) => void, ...args: T): number;
/**
* Aborts the specified macrotask before it's run.
*
* @param handle The ID of the macrotask to remove from the macrotask queue.
*/
declare function clearImmediate(handle: number): void;
@@ -0,0 +1,36 @@
import './index';
// $ExpectType number
const i1 = setImmediate((...args) => {
args; // $ExpectType []
});
// $ExpectType number
const i2 = setImmediate((...args) => {
args; // $ExpectType [number]
}, 1);
// $ExpectType number
const i3 = setImmediate((foo, bar, baz) => {
foo; // $ExpectType number
bar; // $ExpectType string
baz; // $ExpectType boolean
}, 1, 'a', true);
clearImmediate(i1);
clearImmediate(i2);
clearImmediate(i3);
// Errors:
setImmediate(); // $ExpectError
setImmediate((foo: unknown) => {}); // $ExpectError
clearImmediate(); // $ExpectError
clearImmediate(null); // $ExpectError
clearImmediate(undefined); // $ExpectError
clearImmediate('string'); // $ExpectError
clearImmediate(Symbol.iterator); // $ExpectError
clearImmediate(true); // $ExpectError
clearImmediate(false); // $ExpectError
clearImmediate({}); // $ExpectError
clearImmediate(Function); // $ExpectError
+19
View File
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../../",
"typeRoots": ["../../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"setimmediate-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }
+19
View File
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"setimmediate-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }