Skip to content

Commit 6f6e5c7

Browse files
committed
Merge pull request DefinitelyTyped#5827 from jaysoo/add-redux-actions
Add redux-actions
2 parents 8e7c1b5 + f06106d commit 6f6e5c7

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

redux-actions/redux-actions-tests.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// <reference path="./redux-actions.d.ts" />
2+
3+
const minimalAction: ReduxActions.Action = { type: 'INCREMENT' };
4+
const richerAction: ReduxActions.Action = {
5+
type: 'INCREMENT',
6+
payload: 2,
7+
error: false,
8+
meta: {
9+
remote: true
10+
}
11+
};
12+
13+
const incrementAction: (...args: any[]) => ReduxActions.Action = ReduxActions.createAction<number>(
14+
'INCREMENT',
15+
(amount: number) => amount
16+
);
17+
const action: ReduxActions.Action = incrementAction(42);
18+
19+
const incrementByAction: (...args: any[]) => ReduxActions.Action = ReduxActions.createAction<number>(
20+
'INCREMENT_BY',
21+
(amount: number) => amount,
22+
amount => ({ remote: true })
23+
);
24+
25+
let state: number;
26+
27+
const actionHandler = ReduxActions.handleAction<number>(
28+
'INCREMENT',
29+
(state: number, action: ReduxActions.Action) => state + 1
30+
);
31+
state = actionHandler(0, { type: 'INCREMENT' });
32+
33+
const actionHandlerWithReduceMap = ReduxActions.handleAction<number>(
34+
'INCREMENT_BY', {
35+
next(state: number, action: ReduxActions.Action) {
36+
return state + action.payload;
37+
},
38+
throw(state: number) { return state }
39+
}
40+
);
41+
state = actionHandlerWithReduceMap(0, { type: 'INCREMENT' });
42+
43+
const actionsHandler = ReduxActions.handleActions<number>({
44+
'INCREMENT': (state: number, action: ReduxActions.Action) => state + 1,
45+
'DECREMENT': (state: number, action: ReduxActions.Action) => state - 1
46+
});
47+
state = actionsHandler(0, { type: 'INCREMENT' });
48+
49+
const actionsHandlerWithInitialState = ReduxActions.handleActions<number>({
50+
'INCREMENT': (state: number, action: ReduxActions.Action) => state + 1,
51+
'DECREMENT': (state: number, action: ReduxActions.Action) => state - 1
52+
}, 0);
53+
state = actionsHandlerWithInitialState(0, { type: 'INCREMENT' });
54+
55+
56+

redux-actions/redux-actions.d.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Type definitions for redux-actions v0.8.0
2+
// Project: https://github.com/acdlite/redux-actions
3+
// Definitions by: Jack Hsu <https://github.com/jaysoo>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
declare module ReduxActions {
6+
// FSA-compliant action.
7+
// See: https://github.com/acdlite/flux-standard-action
8+
type Action = {
9+
type: string
10+
payload?: any
11+
error?: boolean
12+
meta?: any
13+
};
14+
15+
type PayloadCreator<T> = (...args: any[]) => T;
16+
type MetaCreator = (...args: any[]) => any;
17+
18+
type Reducer<T> = (state: T, action: Action) => T;
19+
20+
type ReducerMap<T> = {
21+
[actionType: string]: Reducer<T>
22+
};
23+
24+
export function createAction<T>(actionType: string, payloadCreator?: PayloadCreator<T>, metaCreator?: MetaCreator): (...args: any[]) => Action;
25+
26+
export function handleAction<T>(actionType: string, reducer: Reducer<T> | ReducerMap<T>): Reducer<T>;
27+
28+
export function handleActions<T>(reducerMap: ReducerMap<T>, initialState?: T): Reducer<T>;
29+
}
30+
31+
declare module 'redux-actions' {
32+
export = ReduxActions;
33+
}
34+

0 commit comments

Comments
 (0)