diff --git a/types/workerpool/index.d.ts b/types/workerpool/index.d.ts index 2bb6ca4b66..65fab7029e 100644 --- a/types/workerpool/index.d.ts +++ b/types/workerpool/index.d.ts @@ -3,7 +3,7 @@ // Definitions by: Alorel // Seulgi Kim // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 3.1 /// @@ -17,6 +17,10 @@ export interface WorkerPoolStats { activeTasks: number; } +export type Proxy any}> = { + [M in keyof T]: (...args: Parameters) => Promise>; +}; + export interface WorkerPool { /** * Execute a function on a worker with given arguments. @@ -27,14 +31,15 @@ export interface WorkerPool { * and executed there with the provided parameters. The provided function must be static, * it must not depend on variables in a surrounding scope. */ - exec(method: ((...args: any[]) => any) | string, params: any[] | null): Promise; + exec any>(method: T | string, params: Parameters | null): Promise>; /** * Create a proxy for the worker pool. * The proxy contains a proxy for all methods available on the worker. * All methods return promises resolving the methods result. */ - proxy(): Promise; + // tslint:disable-next-line: no-unnecessary-generics + proxy any}>(): Promise>; /** Retrieve statistics on workers, and active and pending tasks. */ stats(): WorkerPoolStats; diff --git a/types/workerpool/workerpool-tests.ts b/types/workerpool/workerpool-tests.ts index b1d3c38c4d..703fafaf24 100644 --- a/types/workerpool/workerpool-tests.ts +++ b/types/workerpool/workerpool-tests.ts @@ -37,6 +37,33 @@ pool.exec('foo', null) .then(() => pool.exec('foo', [])) .then(() => pool.exec(() => {}, null)); +function add(a: number, b: number): number { + return a + b; +} + +function hello(): string { + return 'hello'; +} + +pool.exec(add, [1, 2]) + .then((c: number) => c); +pool.exec('add', [1, 2]) + .then((c: number) => c); +pool.exec(hello, []) + .then((s: string) => s); + +const workers = {add, hello}; +type IWorkers = typeof workers; +pool.proxy().then((proxy) => { + proxy.add(1, 2); + proxy.hello(); +}); + +pool.proxy().then((proxy) => { + proxy.add(1, 2); + proxy.hello(); +}); + new wp.Promise.CancellationError(); new wp.Promise.TimeoutError();