Skip to content

Commit cb908ca

Browse files
committed
feat: provide a typescript definition for the project
1 parent 5e8f2a1 commit cb908ca

File tree

5 files changed

+521
-0
lines changed

5 files changed

+521
-0
lines changed

definitions/action.d.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* *** MIT LICENSE ***
3+
* -------------------------------------------------------------------------
4+
* This code may be modified and distributed under the MIT license.
5+
* See the LICENSE file for details.
6+
* -------------------------------------------------------------------------
7+
*
8+
* @summary Definitions for redux-logic
9+
*
10+
* @author Alvis HT Tang <[email protected]>
11+
* @license MIT
12+
* @copyright Copyright (c) 2018 - All Rights Reserved.
13+
* -------------------------------------------------------------------------
14+
*/
15+
16+
//
17+
// ACTION
18+
//
19+
20+
/* * * * * *
21+
| The following definitions are bascially identical to |
22+
| flux-standard-action without an extra package. It also |
23+
| makes use of conditional type to make the type of |
24+
| payload and meta more accurate. |
25+
* * * * * */
26+
27+
/** Action as an agrument */
28+
export type ArgumentAction<
29+
Type extends string = string,
30+
Payload extends object = undefined,
31+
Meta extends object = undefined
32+
> = ActionBasis<Type> & Partial<Action<string, object, object>>;
33+
34+
/** all different types of Action */
35+
export type Action<
36+
Type extends string = string,
37+
Payload extends object = undefined,
38+
Meta extends object = undefined
39+
> =
40+
| ErroneousAction<Type, Meta>
41+
| (StandardAction<Type, Payload, Meta> & { error?: false });
42+
43+
/** Action without any error */
44+
export type StandardAction<
45+
Type extends string = string,
46+
Payload extends object = undefined,
47+
Meta extends object = undefined
48+
> = ActionBasis<Type> & PayloadBasis<Payload> & MetaBasis<Meta>;
49+
50+
/** Action with an Error */
51+
export type ErroneousAction<
52+
Type extends string = string,
53+
Meta extends object = undefined
54+
> = ActionBasis<Type> & PayloadBasis<Error> & MetaBasis<Meta> & { error: true };
55+
56+
/* ----- Auxiliary Types ----- */
57+
58+
/** the most basic action object */
59+
export interface ActionBasis<Type extends string = string> {
60+
type: Type extends infer U ? U : string;
61+
}
62+
63+
/** return an interface with payload only if it presents */
64+
export type PayloadBasis<
65+
Payload extends object = undefined
66+
> = Payload extends undefined ? {} : { payload: Payload };
67+
68+
/** return an interface with meta only if it presents */
69+
export type MetaBasis<Meta extends object = undefined> = Meta extends undefined
70+
? {}
71+
: { meta: Meta };
72+
73+
// ---------------------------------------- //

definitions/index.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* *** MIT LICENSE ***
3+
* -------------------------------------------------------------------------
4+
* This code may be modified and distributed under the MIT license.
5+
* See the LICENSE file for details.
6+
* -------------------------------------------------------------------------
7+
*
8+
* @summary Definitions for redux-logic
9+
*
10+
* @author Alvis HT Tang <[email protected]>
11+
* @license MIT
12+
* @copyright Copyright (c) 2018 - All Rights Reserved.
13+
* -------------------------------------------------------------------------
14+
*/
15+
16+
export * from './action';
17+
export * from './logic';
18+
export * from './middleware';
19+
export * from './utilities';

0 commit comments

Comments
 (0)