Согласно определению Redux для машинописного текста, эти интерфейсы должны быть реализованы для создания промежуточного программного обеспечения:
/* middleware */
export interface MiddlewareAPI<D extends Dispatch = Dispatch, S = any> {
dispatch: D
getState(): S
}
/**
* A middleware is a higher-order function that composes a dispatch function
* to return a new dispatch function. It often turns async actions into
* actions.
*
* Middleware is composable using function composition. It is useful for
* logging actions, performing side effects like routing, or turning an
* asynchronous API call into a series of synchronous actions.
*
* @template DispatchExt Extra Dispatch signature added by this middleware.
* @template S The type of the state supported by this middleware.
* @template D The type of Dispatch of the store where this middleware is
* installed.
*/
export interface Middleware<
DispatchExt = {},
S = any,
D extends Dispatch = Dispatch
> {
(api: MiddlewareAPI<D, S>): (
next: Dispatch<AnyAction>
) => (action: any) => any
}
Я пробовал это:
import { Middleware, Dispatch, AnyAction, MiddlewareAPI } from 'redux';
import { AppState } from 'AppState';
class MiddlewareBase implements Middleware<{}, AppState, Dispatch<AnyAction>> {
constructor() {
return (api: MiddlewareAPI<Dispatch<AnyAction>, AppState>) =>
(next: Dispatch<AnyAction>) =>
(action: AnyAction) =>
{
// TODO: Do something before calling the next middleware.
return next(action);
};
}
}
export default MiddlewareBase;
Но компилятор жалуется на это:
Type 'MiddlewareBase' provides no match for the signature '(api: MiddlewareAPI<Dispatch<AnyAction>, AppState>): (next: Dispatch<AnyAction>) => (action: any) => any'
Обновление:
Это должен быть класс, а не функция.Я создал базовый класс, чтобы потом наследовать их.