UNPKG

4.7 kBTypeScriptView Raw
1export interface AnyAction {
2 type: any;
3}
4export declare type Meta = null | {
5 [key: string]: any;
6};
7export interface Action<Payload> extends AnyAction {
8 type: string;
9 payload: Payload;
10 error?: boolean;
11 meta?: Meta;
12}
13/**
14 * Returns `true` if action has the same type as action creator.
15 * Defines Type Guard that lets TypeScript know `payload` type inside blocks
16 * where `isType` returned `true`.
17 *
18 * @example
19 *
20 * const somethingHappened =
21 * actionCreator<{foo: string}>('SOMETHING_HAPPENED');
22 *
23 * if (isType(action, somethingHappened)) {
24 * // action.payload has type {foo: string}
25 * }
26 */
27export declare function isType<Payload>(action: AnyAction, actionCreator: ActionCreator<Payload>): action is Action<Payload>;
28export interface ActionCreator<Payload> {
29 type: string;
30 /**
31 * Identical to `isType` except it is exposed as a bound method of an action
32 * creator. Since it is bound and takes a single argument it is ideal for
33 * passing to a filtering function like `Array.prototype.filter` or
34 * RxJS's `Observable.prototype.filter`.
35 *
36 * @example
37 *
38 * const somethingHappened =
39 * actionCreator<{foo: string}>('SOMETHING_HAPPENED');
40 * const somethingElseHappened =
41 * actionCreator<{bar: number}>('SOMETHING_ELSE_HAPPENED');
42 *
43 * if (somethingHappened.match(action)) {
44 * // action.payload has type {foo: string}
45 * }
46 *
47 * const actionArray = [
48 * somethingHappened({foo: 'foo'}),
49 * somethingElseHappened({bar: 5}),
50 * ];
51 *
52 * // somethingHappenedArray has inferred type Action<{foo: string}>[]
53 * const somethingHappenedArray =
54 * actionArray.filter(somethingHappened.match);
55 */
56 match: (action: AnyAction) => action is Action<Payload>;
57 /**
58 * Creates action with given payload and metadata.
59 *
60 * @param payload Action payload.
61 * @param meta Action metadata. Merged with `commonMeta` of Action Creator.
62 */
63 (payload: Payload, meta?: Meta): Action<Payload>;
64}
65export declare type Success<Params, Result> = ({
66 params: Params;
67} | (Params extends void ? {
68 params?: Params;
69} : never)) & ({
70 result: Result;
71} | (Result extends void ? {
72 result?: Result;
73} : never));
74export declare type Failure<Params, Error> = ({
75 params: Params;
76} | (Params extends void ? {
77 params?: Params;
78} : never)) & {
79 error: Error;
80};
81export interface AsyncActionCreators<Params, Result, Error = {}> {
82 type: string;
83 started: ActionCreator<Params>;
84 done: ActionCreator<Success<Params, Result>>;
85 failed: ActionCreator<Failure<Params, Error>>;
86}
87export interface ActionCreatorFactory {
88 /**
89 * Creates Action Creator that produces actions with given `type` and payload
90 * of type `Payload`.
91 *
92 * @param type Type of created actions.
93 * @param commonMeta Metadata added to created actions.
94 * @param isError Defines whether created actions are error actions.
95 */
96 <Payload = void>(type: string, commonMeta?: Meta, isError?: boolean): ActionCreator<Payload>;
97 /**
98 * Creates Action Creator that produces actions with given `type` and payload
99 * of type `Payload`.
100 *
101 * @param type Type of created actions.
102 * @param commonMeta Metadata added to created actions.
103 * @param isError Function that detects whether action is error given the
104 * payload.
105 */
106 <Payload = void>(type: string, commonMeta?: Meta, isError?: (payload: Payload) => boolean): ActionCreator<Payload>;
107 /**
108 * Creates three Action Creators:
109 * * `started: ActionCreator<Params>`
110 * * `done: ActionCreator<{params: Params, result: Result}>`
111 * * `failed: ActionCreator<{params: Params, error: Error}>`
112 *
113 * Useful to wrap asynchronous processes.
114 *
115 * @param type Prefix for types of created actions, which will have types
116 * `${type}_STARTED`, `${type}_DONE` and `${type}_FAILED`.
117 * @param commonMeta Metadata added to created actions.
118 */
119 async<Params, Result, Error = {}>(type: string, commonMeta?: Meta): AsyncActionCreators<Params, Result, Error>;
120}
121/**
122 * Creates Action Creator factory with optional prefix for action types.
123 * @param prefix Prefix to be prepended to action types as `<prefix>/<type>`.
124 * @param defaultIsError Function that detects whether action is error given the
125 * payload. Default is `payload => payload instanceof Error`.
126 */
127export declare function actionCreatorFactory(prefix?: string | null, defaultIsError?: (payload: any) => boolean): ActionCreatorFactory;
128export default actionCreatorFactory;
129
\No newline at end of file