add d.ts file for redux-action

This commit is contained in:
newraina
2017-03-20 11:48:49 +08:00
parent 1f6914b860
commit a27cd00392
5 changed files with 205 additions and 0 deletions
+93
View File
@@ -0,0 +1,93 @@
// Type definitions for redux-action 1.2
// Project: https://github.com/coderhaoxin/redux-action
// Definitions by: newraina <https://github.com/newraina>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/**
* inspired by @types/redux-actions, thanks.
*/
import Redux from 'redux';
export as namespace ReduxAction
interface BaseAction {
type: string;
}
interface Action<Payload> extends BaseAction {
payload: Payload;
}
interface MetaAction<Payload, Meta> extends Action<Payload> {
meta: Meta;
}
type ThunkAction<Payload> = (dispatch: Redux.Dispatch<any>, getState: () => any) => Promise<Action<Payload>>;
type ThunkMetaAction<Payload, Meta> = (dispatch: Redux.Dispatch<any>, getState: () => any) => Promise<MetaAction<Payload, Meta>>;
/** argument inferring borrowed from redux-actions definitions */
type ActionFunction0<R> = () => R;
type ActionFunction1<T1, R> = (t1: T1) => R;
type ActionFunction2<T1, T2, R> = (t1: T1, t2: T2) => R;
type ActionFunction3<T1, T2, T3, R> = (t1: T1, t2: T2, t3: T3) => R;
type ActionFunctionAny<R> = (...args: any[]) => R;
type ReducerHandler<State> = <A extends BaseAction, S extends State>(payload: any, state?: State, action?: A) => S;
interface ReducerHandlers<State> {
[type: string]: ReducerHandler<State>;
}
export function createAction<Payload>(
type: string,
payloadCreator: ActionFunctionAny<Promise<Payload> | Payload>,
): ActionFunctionAny<ThunkAction<Payload>>;
export function createAction<Payload>(
payloadCreator: ActionFunctionAny<Promise<Payload> | Payload>,
): ActionFunctionAny<ThunkAction<Payload>>;
export function createAction<Payload, Arg>(
payloadCreator: ActionFunction1<Arg, Promise<Payload> | Payload>,
): ActionFunction1<Arg, ThunkAction<Payload>>;
export function createAction<Payload, Arg1, Arg2>(
payloadCreator: ActionFunction2<Arg1, Arg2, Promise<Payload> | Payload>,
): ActionFunction2<Arg1, Arg2, ThunkAction<Payload>>;
export function createAction<Payload, Arg1, Arg2, Arg3>(
payloadCreator: ActionFunction3<Arg1, Arg2, Arg3, Promise<Payload> | Payload>,
): ActionFunction3<Arg1, Arg2, Arg3, ThunkAction<Payload>>;
export function createAction<Payload, Meta>(
type: string,
payloadCreator: ActionFunctionAny<Promise<Payload> | Payload>,
metaCreator: ActionFunctionAny<Meta>
): ActionFunctionAny<ThunkMetaAction<Payload, Meta>>;
export function createAction<Payload, Meta>(
payloadCreator: ActionFunctionAny<Promise<Payload> | Payload>,
metaCreator: ActionFunctionAny<Meta>
): ActionFunctionAny<ThunkMetaAction<Payload, Meta>>;
export function createAction<Payload>(
type?: string,
): ActionFunction1<any, ThunkAction<Payload>>;
export function createAction<Payload, Arg>(
type?: string,
): ActionFunction1<Arg, ThunkAction<Payload>>;
export function createSyncAction<Payload>(
type: string,
payloadCreator?: ActionFunctionAny<Payload>,
): ActionFunctionAny<Action<Payload>>;
export function createSyncAction<Payload, Meta>(
type: string,
payloadCreator: ActionFunctionAny<Payload>,
metaCreator: ActionFunctionAny<Meta>
): ActionFunctionAny<MetaAction<Payload, Meta>>;
export function createReducer<State>(defaultState: State, handlers: ReducerHandlers<State>): Redux.Reducer<State>;
+5
View File
@@ -0,0 +1,5 @@
{
"dependencies": {
"redux": "^3.6.0"
}
}
+83
View File
@@ -0,0 +1,83 @@
import * as ReduxAction from 'redux-action';
import * as Redux from 'redux';
interface Payload {
name: string;
id: number;
}
const dispatch: Redux.Dispatch<any> = (...args: any[]): any => { };
const actionCreator0 = ReduxAction.createAction<Payload>('get items');
dispatch(actionCreator0(1))
.then(action => {
const type: string = action.type;
const payload: Payload = action.payload;
});
const actionCreator1 = ReduxAction.createAction<Payload>();
dispatch(actionCreator1(1))
.then(action => {
const type: string = action.type;
const payload: Payload = action.payload;
});
const actionCreator2 = ReduxAction.createAction('get items', (name: string) => name);
dispatch(actionCreator2(1))
.then(action => {
const type: string = action.type;
const payload: string = action.payload;
});
const actionCreator3 = ReduxAction.createAction('get items', (name: string) => Promise.resolve(name));
dispatch(actionCreator3(1))
.then(action => {
const type: string = action.type;
const payload: string = action.payload;
});
const actionCreator4 = ReduxAction.createAction((name: string) => name);
dispatch(actionCreator4(1))
.then(action => {
const type: string = action.type;
const payload: string = action.payload;
});
const actionCreator5 = ReduxAction.createAction<number, string>((name: string) => 1);
dispatch(actionCreator5('items'))
.then(action => {
const type: string = action.type;
const payload: number = action.payload;
});
const actionCreator6 = ReduxAction.createAction<number, string, boolean>((name: string) => 1);
dispatch(actionCreator6('items', false))
.then(action => {
const type: string = action.type;
const payload: number = action.payload;
});
const syncActionCreator0 = ReduxAction.createSyncAction<Payload>('get items');
const syncAction0: ReduxAction.Action<Payload> = dispatch(syncActionCreator0(1));
const syncActionCreator1 = ReduxAction.createSyncAction<string>('get items', (name: string) => name);
const syncAction1: ReduxAction.Action<string> = dispatch(syncActionCreator1(1));
interface State {
name: string;
}
const reducer = ReduxAction.createReducer<State>({ name: 'name' }, {
'get items': () => {
return 'new name';
}
});
const combinedReducer: Redux.Reducer<State> = Redux.combineReducers<State>({ reducer });
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"redux-action-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "../tslint.json" }