Added types for mobx-task

This commit is contained in:
Julian Wielga
2019-04-12 16:57:42 +02:00
parent 01fd72c035
commit db7a20126e
4 changed files with 124 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
// Type definitions for mobx-task 1.0
// Project: https://github.com/jeffijoe/mobx-task#readme
// Definitions by: Julian Wielga <https://github.com/JulianWielga>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
export type WithoutPromise<T> = T extends Promise<infer U> ? U : T;
export type TaskState = 'pending' | 'resolved' | 'rejected';
export type TaskError = Error;
export type TaskFunc<R, A extends any[]> = (...args: A) => R;
export interface TaskOptions<Result> {
state?: TaskState;
error?: TaskError;
result?: Result;
swallow?: boolean;
}
export interface TaskMatchProps<Args extends any[], TaskMatchResult, Result = any> {
pending: (...args: Args) => TaskMatchResult;
rejected: (error: TaskError) => TaskMatchResult;
resolved: (result: Result) => TaskMatchResult;
}
export interface TaskStatusAware<Result, Args extends any[]> extends TaskFunc<Promise<Result>, Args> {
readonly state: TaskState;
readonly pending: boolean;
readonly resolved: boolean;
readonly rejected: boolean;
readonly args: Args;
readonly result?: Result;
readonly error?: TaskError;
match<TaskMatchResult>(props: TaskMatchProps<Args, TaskMatchResult, Result>): TaskMatchResult;
wrap<R, A extends any[]>(func: (inner: TaskStatusAware<Result, Args>) => (...args: A) => Promise<R>): TaskStatusAware<R, A>;
setState(props: { state: TaskState, result: Result }): void;
reset(): void;
}
export interface TaskFactory {
<R, A extends any[]>(
func: TaskFunc<R, A>,
options?: TaskOptions<WithoutPromise<R>>,
): TaskStatusAware<WithoutPromise<R>, A>;
resolved<R, A extends any[]>(
func: TaskFunc<R, A>,
options?: Omit<TaskOptions<WithoutPromise<R>>, 'state'>,
): TaskStatusAware<WithoutPromise<R>, A>;
rejected<R, A extends any[]>(
func: TaskFunc<R, A>,
options?: Omit<TaskOptions<WithoutPromise<R>>, 'state'>,
): TaskStatusAware<WithoutPromise<R>, A>;
}
export const task: TaskFactory;
+38
View File
@@ -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<string>
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
+23
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }