mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-17 07:20:23 +00:00
mobx-task: Provides its own types (#39465)
This commit is contained in:
committed by
Jesse Trinity
parent
10ac63b0ff
commit
7f9e0b901f
@@ -2160,6 +2160,12 @@
|
||||
"sourceRepoURL": "https://github.com/will-stone/mobx-cookie",
|
||||
"asOfVersion": "1.1.1"
|
||||
},
|
||||
{
|
||||
"libraryName": "mobx-task",
|
||||
"typingsPackageName": "mobx-task",
|
||||
"sourceRepoURL": "https://github.com/jeffijoe/mobx-task#readme",
|
||||
"asOfVersion": "2.0.0"
|
||||
},
|
||||
{
|
||||
"libraryName": "mockingoose",
|
||||
"typingsPackageName": "mockingoose",
|
||||
|
||||
Vendored
-58
@@ -1,58 +0,0 @@
|
||||
// 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<T1, T2, T3, Args extends any[], Result = any> {
|
||||
pending: (...args: Args) => T1;
|
||||
rejected: (error: TaskError) => T2;
|
||||
resolved: (result: Result) => T3;
|
||||
}
|
||||
|
||||
export interface TaskStatusAware<Result = any, Args extends any[] = 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<PendingType, RejectedType, ResolvedType>(props: TaskMatchProps<PendingType, RejectedType, ResolvedType, Args, Result>): PendingType | RejectedType | ResolvedType;
|
||||
|
||||
wrap<R, A extends any[]>(func: (inner: TaskStatusAware<Result, Args>) => (...args: A) => Promise<R>): TaskStatusAware<R, A>;
|
||||
|
||||
setState(props: TaskOptions<Result>): void;
|
||||
|
||||
reset(): void;
|
||||
}
|
||||
|
||||
export interface TaskCreator<K extends keyof TaskOptions<any>> extends MethodDecorator, PropertyDecorator {
|
||||
<R, A extends any[]>(func: TaskFunc<R, A>, options?: Pick<TaskOptions<WithoutPromise<R>>, K>): TaskStatusAware<WithoutPromise<R>, A>;
|
||||
|
||||
(options: Pick<TaskOptions<WithoutPromise<any>>, K>): PropertyDecorator;
|
||||
(options: Pick<TaskOptions<WithoutPromise<any>>, K>): MethodDecorator;
|
||||
}
|
||||
|
||||
export interface TaskFactory extends TaskCreator<keyof TaskOptions<any>> {
|
||||
resolved: TaskCreator<Exclude<keyof TaskOptions<any>, 'state'>>;
|
||||
rejected: TaskCreator<Exclude<keyof TaskOptions<any>, 'state'>>;
|
||||
}
|
||||
|
||||
export type Task<Result = any, Args extends any[] = any[]> = TaskStatusAware<Result, Args>;
|
||||
export const task: TaskFactory;
|
||||
@@ -1,55 +0,0 @@
|
||||
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,
|
||||
});
|
||||
|
||||
// $ExpectType string | number
|
||||
task1.match({
|
||||
resolved: res => 1,
|
||||
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
|
||||
|
||||
class TestClass {
|
||||
@task fieldWithArrowFn = () => {};
|
||||
@task({ swallow: true }) fieldWithArrowFn2 = () => {};
|
||||
fieldWithArrowFn3 = task(() => {}, { swallow: true });
|
||||
|
||||
@task method() {}
|
||||
|
||||
@task({ swallow: true }) method2() {}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"experimentalDecorators": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"mobx-task-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user