[redux-promise-listener] add types (#38744)

This commit is contained in:
Hikiko
2019-10-03 02:30:34 +06:00
committed by Ryan Cavanaugh
parent e39fd75419
commit 74937467de
5 changed files with 117 additions and 0 deletions

34
types/redux-promise-listener/index.d.ts vendored Normal file
View File

@@ -0,0 +1,34 @@
// Type definitions for redux-promise-listener 1.1
// Project: https://github.com/erikras/redux-promise-listener
// Definitions by: hikiko4ern <https://github.com/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<any>;
unsubscribe: () => void;
}
interface PromiseListener {
middleware: Middleware<{}, AnyAction>;
createAsyncFunction: (config: Config) => AsyncFunction;
}
function createListener(): PromiseListener;
}

View File

@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"redux": "^4.0.0"
}
}

View File

@@ -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<any>
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
});

View File

@@ -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"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }