Add redux-action-utils

This commit is contained in:
tkqubo
2015-09-21 14:36:42 +09:00
parent 09e37435ff
commit f8c20ee00f
2 changed files with 99 additions and 0 deletions
@@ -0,0 +1,62 @@
/// <reference path="redux-action-utils.d.ts" />
/// <reference path="../redux/redux.d.ts" />
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<ImportLesson>(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<UpdateLesson>(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;
+37
View File
@@ -0,0 +1,37 @@
// Type definitions for redux-action-utils 2.0.0
// Project: https://github.com/insin/redux-action-utils
// Definitions by: Qubo <https://github.com/tkqubo>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module "redux-action-utils" {
export interface Action {
type: string;
}
export interface ActionCreator<T> {
(...data: any[]): Action & T;
}
export interface OptionsActionCreator<T> {
(data: T): Action & T;
}
/**
* Creates an action creator which will create an action object with the given type.
*/
export function actionCreator<T>(type: string, ...props: string[]): ActionCreator<T>;
/**
* Creates an action creator which will create an action object with the given type.
*/
export function actionCreator<T>(type: string, props: string[]): ActionCreator<T>;
/**
* Creates an action creator which takes a single object argument and adds its properties to the action object.
*/
export function optionsActionCreator<T>(type: string, ...props: string[]): OptionsActionCreator<T>;
/**
* Creates an action creator which takes a single object argument and adds its properties to the action object.
*/
export function optionsActionCreator<T>(type: string, props: string[]): OptionsActionCreator<T>;
}