diff --git a/types/task-worklet/index.d.ts b/types/task-worklet/index.d.ts new file mode 100644 index 0000000000..add9b1b3dc --- /dev/null +++ b/types/task-worklet/index.d.ts @@ -0,0 +1,36 @@ +// Type definitions for task-worklet 0.1 +// Project: https://github.com/GoogleChromeLabs/task-worklet +// Definitions by: Karol Majewski +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 + +declare class TaskQueue { + constructor(options?: Options); + postTask(taskName: U['name'], ...args: Parameters): Task>; + addModule(moduleURL: string): Promise; +} + +interface Options { + size?: number; +} + +interface TaskDescriptor { + name: string; + (...args: any): any; +} + +export interface Task { + id: number; + state: State; + result: Promise; +} + +export type State = + | 'cancelled' + | 'completed' + | 'fulfilled' + | 'pending' + | 'scheduled'; + +export default TaskQueue; +export as namespace TaskQueue; diff --git a/types/task-worklet/task-worklet-tests.ts b/types/task-worklet/task-worklet-tests.ts new file mode 100644 index 0000000000..30a5072a2b --- /dev/null +++ b/types/task-worklet/task-worklet-tests.ts @@ -0,0 +1,23 @@ +import TaskQueue, { Task, State } from 'task-worklet'; + +interface Fetcher { + name: 'fetch'; + (input: RequestInfo, init?: RequestInit): Promise; +} + +const unsafe = new TaskQueue(); // $ExpectType TaskQueue +const withEmptyOptions = new TaskQueue({}); // $ExpectType TaskQueue +const withValidOptions = new TaskQueue({ size: 2 }); // $ExpectType TaskQueue + +const queue = new TaskQueue(); // $ExpectType TaskQueue +queue.addModule('/fetcher-worklet.js'); // $ExpectType Promise +const task = queue.postTask('fetch', 'https://example.com'); // $ExpectType Task> + +async () => { + const result = await task.result; // $ExpectType Response + const id = task.id; // $ExpectType number + const state = task.state; // $ExpectType State +}; + +new TaskQueue({ size: 1, excessProperty: true }); // $ExpectError +queue.postTask('incorrect-task-name'); // $ExpectError diff --git a/types/task-worklet/tsconfig.json b/types/task-worklet/tsconfig.json new file mode 100644 index 0000000000..4697c07f30 --- /dev/null +++ b/types/task-worklet/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "strictFunctionTypes": true + }, + "files": [ + "index.d.ts", + "task-worklet-tests.ts" + ] +} diff --git a/types/task-worklet/tslint.json b/types/task-worklet/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/task-worklet/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }