diff --git a/types/redux-promise-listener/index.d.ts b/types/redux-promise-listener/index.d.ts new file mode 100644 index 0000000000..a7d3a0367f --- /dev/null +++ b/types/redux-promise-listener/index.d.ts @@ -0,0 +1,34 @@ +// Type definitions for redux-promise-listener 1.1 +// Project: https://github.com/erikras/redux-promise-listener +// Definitions by: hikiko4ern +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.1 + +import { AnyAction, Middleware } from 'redux'; + +export default ReduxPromiseListener.createListener; + +declare namespace ReduxPromiseListener { + type ActionMatcher = (action: AnyAction) => boolean; + + interface Config { + start: string; + resolve: string | ActionMatcher; + reject: string | ActionMatcher; + setPayload?: (action: AnyAction, payload: any) => AnyAction; + getPayload?: (action: AnyAction) => any; + getError?: (action: AnyAction) => any; + } + + interface AsyncFunction { + asyncFunction: (payload?: any) => Promise; + unsubscribe: () => void; + } + + interface PromiseListener { + middleware: Middleware<{}, AnyAction>; + createAsyncFunction: (config: Config) => AsyncFunction; + } + + function createListener(): PromiseListener; +} diff --git a/types/redux-promise-listener/package.json b/types/redux-promise-listener/package.json new file mode 100644 index 0000000000..7f5b19d45b --- /dev/null +++ b/types/redux-promise-listener/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "redux": "^4.0.0" + } +} diff --git a/types/redux-promise-listener/redux-promise-listener-tests.ts b/types/redux-promise-listener/redux-promise-listener-tests.ts new file mode 100644 index 0000000000..7f15826dba --- /dev/null +++ b/types/redux-promise-listener/redux-promise-listener-tests.ts @@ -0,0 +1,53 @@ +import { createStore, applyMiddleware, AnyAction } from 'redux'; +import createReduxPromiseListener from 'redux-promise-listener'; + +// $ExpectType PromiseListener +const reduxPromiseListener = createReduxPromiseListener(); +export const promiseListener = reduxPromiseListener; + +declare var rootReducer: (state: any, action: any) => any; +const store = createStore(rootReducer, applyMiddleware(reduxPromiseListener.middleware)); + +declare const actionMatcher: (action: AnyAction) => boolean; + +const API_REQUEST = 'API_REQUEST'; +const API_SUCCESS = Math.random() > 0.5 ? 'API_SUCCESS' : actionMatcher; +const API_FAILURE = Math.random() > 0.5 ? 'API_FAILURE' : actionMatcher; + +// $ExpectType AsyncFunction +promiseListener.createAsyncFunction({ + start: API_REQUEST, + resolve: API_SUCCESS, + reject: API_FAILURE, +}); + +// $ExpectType AsyncFunction +const action = promiseListener.createAsyncFunction({ + start: API_REQUEST, + resolve: API_SUCCESS, + reject: API_FAILURE, + setPayload: action => ({ + ...action, + input: 'additional data', + }), + getPayload: action => action.result, + getError: action => action.errors, +}); + +// $ExpectType Promise +action.asyncFunction('test'); + +// $ExpectType void +action.unsubscribe(); + +// $ExpectError +promiseListener.createAsyncFunction(); + +// $ExpectError +promiseListener.createAsyncFunction("I'm not a config"); + +promiseListener.createAsyncFunction({ + start: API_REQUEST, + resolve: 0, // $ExpectError + reject: null, // $ExpectError +}); diff --git a/types/redux-promise-listener/tsconfig.json b/types/redux-promise-listener/tsconfig.json new file mode 100644 index 0000000000..2ba35656a7 --- /dev/null +++ b/types/redux-promise-listener/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "redux-promise-listener-tests.ts" + ] +} diff --git a/types/redux-promise-listener/tslint.json b/types/redux-promise-listener/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/redux-promise-listener/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }