mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-07-01 15:50:13 +00:00
[redux-promise-listener] add types (#38744)
This commit is contained in:
34
types/redux-promise-listener/index.d.ts
vendored
Normal file
34
types/redux-promise-listener/index.d.ts
vendored
Normal 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;
|
||||
}
|
||||
6
types/redux-promise-listener/package.json
Normal file
6
types/redux-promise-listener/package.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"redux": "^4.0.0"
|
||||
}
|
||||
}
|
||||
53
types/redux-promise-listener/redux-promise-listener-tests.ts
Normal file
53
types/redux-promise-listener/redux-promise-listener-tests.ts
Normal 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
|
||||
});
|
||||
23
types/redux-promise-listener/tsconfig.json
Normal file
23
types/redux-promise-listener/tsconfig.json
Normal 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"
|
||||
]
|
||||
}
|
||||
1
types/redux-promise-listener/tslint.json
Normal file
1
types/redux-promise-listener/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user