diff --git a/redux-actions/redux-actions-tests.ts b/redux-actions/redux-actions-tests.ts new file mode 100644 index 0000000000..30dd00a970 --- /dev/null +++ b/redux-actions/redux-actions-tests.ts @@ -0,0 +1,56 @@ +/// + +const minimalAction: ReduxActions.Action = { type: 'INCREMENT' }; +const richerAction: ReduxActions.Action = { + type: 'INCREMENT', + payload: 2, + error: false, + meta: { + remote: true + } +}; + +const incrementAction: (...args: any[]) => ReduxActions.Action = ReduxActions.createAction( + 'INCREMENT', + (amount: number) => amount +); +const action: ReduxActions.Action = incrementAction(42); + +const incrementByAction: (...args: any[]) => ReduxActions.Action = ReduxActions.createAction( + 'INCREMENT_BY', + (amount: number) => amount, + amount => ({ remote: true }) +); + +let state: number; + +const actionHandler = ReduxActions.handleAction( + 'INCREMENT', + (state: number, action: ReduxActions.Action) => state + 1 +); +state = actionHandler(0, { type: 'INCREMENT' }); + +const actionHandlerWithReduceMap = ReduxActions.handleAction( + 'INCREMENT_BY', { + next(state: number, action: ReduxActions.Action) { + return state + action.payload; + }, + throw(state: number) { return state } + } +); +state = actionHandlerWithReduceMap(0, { type: 'INCREMENT' }); + +const actionsHandler = ReduxActions.handleActions({ + 'INCREMENT': (state: number, action: ReduxActions.Action) => state + 1, + 'DECREMENT': (state: number, action: ReduxActions.Action) => state - 1 +}); +state = actionsHandler(0, { type: 'INCREMENT' }); + +const actionsHandlerWithInitialState = ReduxActions.handleActions({ + 'INCREMENT': (state: number, action: ReduxActions.Action) => state + 1, + 'DECREMENT': (state: number, action: ReduxActions.Action) => state - 1 +}, 0); +state = actionsHandlerWithInitialState(0, { type: 'INCREMENT' }); + + + diff --git a/redux-actions/redux-actions.d.ts b/redux-actions/redux-actions.d.ts new file mode 100644 index 0000000000..522f184625 --- /dev/null +++ b/redux-actions/redux-actions.d.ts @@ -0,0 +1,34 @@ +// Type definitions for redux-actions v0.8.0 +// Project: https://github.com/acdlite/redux-actions +// Definitions by: Jack Hsu +// Definitions: https://github.com/borisyankov/DefinitelyTyped +declare module ReduxActions { + // FSA-compliant action. + // See: https://github.com/acdlite/flux-standard-action + type Action = { + type: string + payload?: any + error?: boolean + meta?: any + }; + + type PayloadCreator = (...args: any[]) => T; + type MetaCreator = (...args: any[]) => any; + + type Reducer = (state: T, action: Action) => T; + + type ReducerMap = { + [actionType: string]: Reducer + }; + + export function createAction(actionType: string, payloadCreator?: PayloadCreator, metaCreator?: MetaCreator): (...args: any[]) => Action; + + export function handleAction(actionType: string, reducer: Reducer | ReducerMap): Reducer; + + export function handleActions(reducerMap: ReducerMap, initialState?: T): Reducer; +} + +declare module 'redux-actions' { + export = ReduxActions; +} +