From db7a20126eaec511ac5f6c429e7d5c6446324b51 Mon Sep 17 00:00:00 2001 From: Julian Wielga Date: Fri, 12 Apr 2019 16:57:42 +0200 Subject: [PATCH] Added types for mobx-task --- types/mobx-task/index.d.ts | 62 ++++++++++++++++++++++++++++++ types/mobx-task/mobx-task-tests.ts | 38 ++++++++++++++++++ types/mobx-task/tsconfig.json | 23 +++++++++++ types/mobx-task/tslint.json | 1 + 4 files changed, 124 insertions(+) create mode 100644 types/mobx-task/index.d.ts create mode 100644 types/mobx-task/mobx-task-tests.ts create mode 100644 types/mobx-task/tsconfig.json create mode 100644 types/mobx-task/tslint.json diff --git a/types/mobx-task/index.d.ts b/types/mobx-task/index.d.ts new file mode 100644 index 0000000000..d44e515a0e --- /dev/null +++ b/types/mobx-task/index.d.ts @@ -0,0 +1,62 @@ +// Type definitions for mobx-task 1.0 +// Project: https://github.com/jeffijoe/mobx-task#readme +// Definitions by: Julian Wielga +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 + +export type Omit = Pick>; +export type WithoutPromise = T extends Promise ? U : T; + +export type TaskState = 'pending' | 'resolved' | 'rejected'; +export type TaskError = Error; +export type TaskFunc = (...args: A) => R; + +export interface TaskOptions { + state?: TaskState; + error?: TaskError; + result?: Result; + swallow?: boolean; +} + +export interface TaskMatchProps { + pending: (...args: Args) => TaskMatchResult; + rejected: (error: TaskError) => TaskMatchResult; + resolved: (result: Result) => TaskMatchResult; +} + +export interface TaskStatusAware extends TaskFunc, Args> { + readonly state: TaskState; + readonly pending: boolean; + readonly resolved: boolean; + readonly rejected: boolean; + readonly args: Args; + readonly result?: Result; + readonly error?: TaskError; + + match(props: TaskMatchProps): TaskMatchResult; + + wrap(func: (inner: TaskStatusAware) => (...args: A) => Promise): TaskStatusAware; + + setState(props: { state: TaskState, result: Result }): void; + + reset(): void; +} + +export interface TaskFactory { + ( + func: TaskFunc, + options?: TaskOptions>, + ): TaskStatusAware, A>; + + resolved( + func: TaskFunc, + options?: Omit>, 'state'>, + ): TaskStatusAware, A>; + + rejected( + func: TaskFunc, + options?: Omit>, 'state'>, + ): TaskStatusAware, A>; +} + +export const task: TaskFactory; diff --git a/types/mobx-task/mobx-task-tests.ts b/types/mobx-task/mobx-task-tests.ts new file mode 100644 index 0000000000..8f27a141e4 --- /dev/null +++ b/types/mobx-task/mobx-task-tests.ts @@ -0,0 +1,38 @@ +import { task } from 'mobx-task'; + +const func = () => 42; +const funcAsync = (txt: string, num: number) => Promise.resolve(`${txt}${num}`); + +const task1 = task(funcAsync); + +task1('test', 123); // $ExpectType Promise +task1.result; // $ExpectType string | undefined +task1.state; // $ExpectType TaskState + +// $ExpectType number +task1.match({ + resolved: res => 1, + pending: (txt, num) => num, + rejected: error => -1, +}); + +// $ExpectType string +task1.match({ + resolved: res => res, + pending: (txt, num) => txt, + rejected: error => error.message, +}); + +// wrap +const task2 = task(func); +const task2ToString = task2.wrap(inner => { + return () => { + return inner().then(r => JSON.stringify(r)); + }; +}); +task2ToString.result; // $ExpectType string | undefined + +// options +const task3 = task(func, { result: 1, state: 'resolved' }); +task3.setState({ result: 2, state: 'pending' }); +task3.setState({ result: '2', state: 'pending' }); // $ExpectError diff --git a/types/mobx-task/tsconfig.json b/types/mobx-task/tsconfig.json new file mode 100644 index 0000000000..dd74ced6ba --- /dev/null +++ b/types/mobx-task/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "mobx-task-tests.ts" + ] +} diff --git a/types/mobx-task/tslint.json b/types/mobx-task/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/mobx-task/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }