diff --git a/types/setimmediate/index.d.ts b/types/setimmediate/index.d.ts new file mode 100644 index 0000000000..671ea27306 --- /dev/null +++ b/types/setimmediate/index.d.ts @@ -0,0 +1,28 @@ +// Type definitions for setimmediate 1.0 +// Project: https://github.com/yuzujs/setImmediate#readme +// Definitions by: 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; diff --git a/types/setimmediate/package.json b/types/setimmediate/package.json new file mode 100644 index 0000000000..68998db91b --- /dev/null +++ b/types/setimmediate/package.json @@ -0,0 +1,11 @@ +{ + "private": true, + "types": "index", + "typesVersions": { + ">=3.1.0-0": { + "*": [ + "ts3.1/*" + ] + } + } +} diff --git a/types/setimmediate/setimmediate-tests.ts b/types/setimmediate/setimmediate-tests.ts new file mode 100644 index 0000000000..4aace1ba70 --- /dev/null +++ b/types/setimmediate/setimmediate-tests.ts @@ -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 diff --git a/types/setimmediate/ts3.1/index.d.ts b/types/setimmediate/ts3.1/index.d.ts new file mode 100644 index 0000000000..333b0b3981 --- /dev/null +++ b/types/setimmediate/ts3.1/index.d.ts @@ -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(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; diff --git a/types/setimmediate/ts3.1/setimmediate-tests.ts b/types/setimmediate/ts3.1/setimmediate-tests.ts new file mode 100644 index 0000000000..2e836df107 --- /dev/null +++ b/types/setimmediate/ts3.1/setimmediate-tests.ts @@ -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 diff --git a/types/setimmediate/ts3.1/tsconfig.json b/types/setimmediate/ts3.1/tsconfig.json new file mode 100644 index 0000000000..a85d0daa9f --- /dev/null +++ b/types/setimmediate/ts3.1/tsconfig.json @@ -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" + ] +} diff --git a/types/setimmediate/ts3.1/tslint.json b/types/setimmediate/ts3.1/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/setimmediate/ts3.1/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/setimmediate/tsconfig.json b/types/setimmediate/tsconfig.json new file mode 100644 index 0000000000..acb0495fab --- /dev/null +++ b/types/setimmediate/tsconfig.json @@ -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" + ] +} diff --git a/types/setimmediate/tslint.json b/types/setimmediate/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/setimmediate/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }