From f8c20ee00f60c4fd3f07da9ccb416ba9b7f0842e Mon Sep 17 00:00:00 2001 From: tkqubo Date: Mon, 21 Sep 2015 14:36:42 +0900 Subject: [PATCH] Add redux-action-utils --- .../redux-action-utils-tests.ts | 62 +++++++++++++++++++ redux-action-utils/redux-action-utils.d.ts | 37 +++++++++++ 2 files changed, 99 insertions(+) create mode 100644 redux-action-utils/redux-action-utils-tests.ts create mode 100644 redux-action-utils/redux-action-utils.d.ts diff --git a/redux-action-utils/redux-action-utils-tests.ts b/redux-action-utils/redux-action-utils-tests.ts new file mode 100644 index 0000000000..d3994a1534 --- /dev/null +++ b/redux-action-utils/redux-action-utils-tests.ts @@ -0,0 +1,62 @@ +/// +/// + +import { actionCreator, optionsActionCreator } from 'redux-action-utils'; +import { Action, ActionCreator } from 'redux-action-utils'; + +let types = { + ADD_LESSON: 'ADD_LESSON', + IMPORT_LESSONS: 'IMPORT_LESSONS', + UPDATE_LESSON: 'UPDATE_LESSON' +}; +var type: string; + +export default { + addLesson: actionCreator(types.ADD_LESSON), + importLessons: actionCreator(types.IMPORT_LESSONS, 'lessons'), + updateLesson: optionsActionCreator(types.UPDATE_LESSON, 'id', 'update') +}; + +var ac = actionCreator(types.ADD_LESSON); +var action: Action = ac(); +type = action.type; + + +class ImportLesson { + lessons: string[]; +} + +const importLessonAction = actionCreator(types.IMPORT_LESSONS, 'lessons'); +var importLesson = importLessonAction(['lesson 1', 'lesson 2']); +// → {type: 'IMPORT_LESSONS', lessons: ['lesson 1', 'lesson 2']} + +var lessons: string[] = importLesson.lessons; +type = importLesson.type; + + +class UpdateLesson { + id: number; + update: { + text: string + } +} + +const updateLessonAction = optionsActionCreator(types.UPDATE_LESSON, 'id', 'update'); +let updateLesson = updateLessonAction({ + id: 1, + update: { + text: '## Lesson 1' + } +}); +/* → + { + type: 'UPDATE_LESSON', + id: 1, + update: { + text: '## Lesson 1' + } + } + */ + +let id: number = updateLesson.id; +type = updateLesson.type; diff --git a/redux-action-utils/redux-action-utils.d.ts b/redux-action-utils/redux-action-utils.d.ts new file mode 100644 index 0000000000..8e565902de --- /dev/null +++ b/redux-action-utils/redux-action-utils.d.ts @@ -0,0 +1,37 @@ +// Type definitions for redux-action-utils 2.0.0 +// Project: https://github.com/insin/redux-action-utils +// Definitions by: Qubo +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "redux-action-utils" { + export interface Action { + type: string; + } + + export interface ActionCreator { + (...data: any[]): Action & T; + } + + export interface OptionsActionCreator { + (data: T): Action & T; + } + + /** + * Creates an action creator which will create an action object with the given type. + */ + export function actionCreator(type: string, ...props: string[]): ActionCreator; + /** + * Creates an action creator which will create an action object with the given type. + */ + export function actionCreator(type: string, props: string[]): ActionCreator; + + /** + * Creates an action creator which takes a single object argument and adds its properties to the action object. + */ + export function optionsActionCreator(type: string, ...props: string[]): OptionsActionCreator; + /** + * Creates an action creator which takes a single object argument and adds its properties to the action object. + */ + export function optionsActionCreator(type: string, props: string[]): OptionsActionCreator; +} +