-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript: type definitions for async middleware #2602
Comments
Actually, my suggestion isn't very good, because it complicates more simple uses. If you have: const maybeVoid: Middleware = store => next => action => {
if (action.type === 'IgnoreMe') {
return
}
next(action)
} TypeScript throws an error, because ERROR in [at-loader] ./src/index.tsx:33:14
TS2339: Property 'type' does not exist on type 'A | Promise<A>'.
Property 'type' does not exist on type 'Promise<A>'. |
Maybe something with generics? Like: export interface MiddlewareDispatch<S, A> {
(action: A): A;
}
export interface MiddlewareAPI<S, A> {
dispatch: MiddlewareDispatch<S, A>;
getState(): S;
}
export interface Middleware<S, A> {
(api: MiddlewareAPI<S, A>): (next: MiddlewareDispatch<S, A>) => MiddlewareDispatch<S, A>;
} That supports both of the previous middleware examples: const vanillaPromise: Middleware<State, Action | Promise<Action>> =
store => next => action => {
if (typeof (action as Promise<Action>).then !== 'function') {
return next(action)
}
return Promise.resolve(action).then(store.dispatch)
}
const maybeVoid: Middleware<State, Action | void> =
store => next => action => {
if (!action) return
if (action.type === 'IgnoreMe') {
return
}
next(action)
} |
The current definition for export interface Middleware {
<S>(api: MiddlewareAPI<S>): (next: Dispatch<S>) => Dispatch<S>;
} This declares that a |
@alexburner Does the typing on the |
Closing due to inactivity. |
@timdorr this thread was closed, but is currently there any alternative or fix to this issue? I am having the exact same problem. |
Check out the typings in 4.0. |
Is there any documentation explaining the proper way to use types for custom middlewares? |
I've also been looking for an redux-thunk middleware strict integration example for a reasonable amount of time, but no luck yet. |
Do you want to request a feature or report a bug?
Bug?
What is the current behavior?
In the definitions file, the interface for Middleware is:
Since the final function in the middleware thunk is typed as
Dispatch<S>
, it is required to synchronously return anA
action. However, custom middleware should support other behaviors. In the definitions file:If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar.
If I attempt to add a type to the
vanillaPromise
from the middleware examples:TypeScript throws the following errors:
What is the expected behavior?
I'd expect the Middleware interface to allow returning a promise-wrapped action, or void, or etc. I'm not sure the actual fix for this... Maybe something like:
This solves the type errors for the
vanillaPromise
example, and would also solve the problems with returning void. However I'm not certain it would cover all middleware use cases.Which versions of Redux, and which browser and OS are affected by this issue? Did this work in previous versions of Redux?
I'm using redux
3.7.2
and typescript2.5.2
. This is the first time I've tried this.The text was updated successfully, but these errors were encountered: