From a27cd00392fda8212f7911ae060cf0366e254bf3 Mon Sep 17 00:00:00 2001 From: newraina Date: Mon, 20 Mar 2017 11:48:49 +0800 Subject: [PATCH] add d.ts file for redux-action --- redux-action/index.d.ts | 93 ++++++++++++++++++++++++++++++ redux-action/package.json | 5 ++ redux-action/redux-action-tests.ts | 83 ++++++++++++++++++++++++++ redux-action/tsconfig.json | 23 ++++++++ redux-action/tslint.json | 1 + 5 files changed, 205 insertions(+) create mode 100644 redux-action/index.d.ts create mode 100644 redux-action/package.json create mode 100644 redux-action/redux-action-tests.ts create mode 100644 redux-action/tsconfig.json create mode 100644 redux-action/tslint.json diff --git a/redux-action/index.d.ts b/redux-action/index.d.ts new file mode 100644 index 0000000000..212b3540a9 --- /dev/null +++ b/redux-action/index.d.ts @@ -0,0 +1,93 @@ +// Type definitions for redux-action 1.2 +// Project: https://github.com/coderhaoxin/redux-action +// Definitions by: 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 extends BaseAction { + payload: Payload; +} + +interface MetaAction extends Action { + meta: Meta; +} + +type ThunkAction = (dispatch: Redux.Dispatch, getState: () => any) => Promise>; +type ThunkMetaAction = (dispatch: Redux.Dispatch, getState: () => any) => Promise>; + +/** argument inferring borrowed from redux-actions definitions */ +type ActionFunction0 = () => R; +type ActionFunction1 = (t1: T1) => R; +type ActionFunction2 = (t1: T1, t2: T2) => R; +type ActionFunction3 = (t1: T1, t2: T2, t3: T3) => R; +type ActionFunctionAny = (...args: any[]) => R; + +type ReducerHandler = (payload: any, state?: State, action?: A) => S; + +interface ReducerHandlers { + [type: string]: ReducerHandler; +} + +export function createAction( + type: string, + payloadCreator: ActionFunctionAny | Payload>, +): ActionFunctionAny>; + +export function createAction( + payloadCreator: ActionFunctionAny | Payload>, +): ActionFunctionAny>; + +export function createAction( + payloadCreator: ActionFunction1 | Payload>, +): ActionFunction1>; + +export function createAction( + payloadCreator: ActionFunction2 | Payload>, +): ActionFunction2>; + +export function createAction( + payloadCreator: ActionFunction3 | Payload>, +): ActionFunction3>; + +export function createAction( + type: string, + payloadCreator: ActionFunctionAny | Payload>, + metaCreator: ActionFunctionAny +): ActionFunctionAny>; + +export function createAction( + payloadCreator: ActionFunctionAny | Payload>, + metaCreator: ActionFunctionAny +): ActionFunctionAny>; + +export function createAction( + type?: string, +): ActionFunction1>; + +export function createAction( + type?: string, +): ActionFunction1>; + +export function createSyncAction( + type: string, + payloadCreator?: ActionFunctionAny, +): ActionFunctionAny>; + +export function createSyncAction( + type: string, + payloadCreator: ActionFunctionAny, + metaCreator: ActionFunctionAny +): ActionFunctionAny>; + +export function createReducer(defaultState: State, handlers: ReducerHandlers): Redux.Reducer; diff --git a/redux-action/package.json b/redux-action/package.json new file mode 100644 index 0000000000..36ce503807 --- /dev/null +++ b/redux-action/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "redux": "^3.6.0" + } +} diff --git a/redux-action/redux-action-tests.ts b/redux-action/redux-action-tests.ts new file mode 100644 index 0000000000..a32b73aa74 --- /dev/null +++ b/redux-action/redux-action-tests.ts @@ -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 = (...args: any[]): any => { }; + +const actionCreator0 = ReduxAction.createAction('get items'); + +dispatch(actionCreator0(1)) + .then(action => { + const type: string = action.type; + const payload: Payload = action.payload; + }); + +const actionCreator1 = ReduxAction.createAction(); + +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((name: string) => 1); + +dispatch(actionCreator5('items')) + .then(action => { + const type: string = action.type; + const payload: number = action.payload; + }); + +const actionCreator6 = ReduxAction.createAction((name: string) => 1); + +dispatch(actionCreator6('items', false)) + .then(action => { + const type: string = action.type; + const payload: number = action.payload; + }); + +const syncActionCreator0 = ReduxAction.createSyncAction('get items'); +const syncAction0: ReduxAction.Action = dispatch(syncActionCreator0(1)); + +const syncActionCreator1 = ReduxAction.createSyncAction('get items', (name: string) => name); +const syncAction1: ReduxAction.Action = dispatch(syncActionCreator1(1)); + +interface State { + name: string; +} +const reducer = ReduxAction.createReducer({ name: 'name' }, { + 'get items': () => { + return 'new name'; + } +}); + +const combinedReducer: Redux.Reducer = Redux.combineReducers({ reducer }); diff --git a/redux-action/tsconfig.json b/redux-action/tsconfig.json new file mode 100644 index 0000000000..0acc89b041 --- /dev/null +++ b/redux-action/tsconfig.json @@ -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" + ] +} diff --git a/redux-action/tslint.json b/redux-action/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/redux-action/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" }