-
Notifications
You must be signed in to change notification settings - Fork 1k
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 Error in Middleware #82
Comments
The current implementation of the typings makes it impossible to create a middleware that only handles standard actions. This means a middleware must handle thunked actions even though it never actually handles them during runtime. (see issue and examples in reduxjs#82) By declaring the Dispatch and Store interfaces again instead of overloading the original implementation of redux a consumer of redux-thunk can choose the right implementation for his use case (e.g. ActionCreator vs. Middleware). fixes reduxjs#82
I think something is missing here. You do need to augment the |
@unional Yeah, I didn't really explain this case. You can see in my PR that the import thunk, { Dispatch, ThunkAction, Store } from '../index.d.ts';
declare const store: Store<{foo: string}>;
store.dispatch((dispatch, getState, extraArg) => {
console.log(extraArg);
}); This is of course a very synthetic example. In a real application you would probably have something like a import {applyMiddleware, createStore} from 'redux';
import thunk, {Store} from 'redux-thunk';
import rootReducer, {IAppState} from './rootReducer';
export default function configureStore(initialState: IAppState): Store<IAppState> {
// You could also configure devtools here and compose with middleware enhancer.
return createStore(rootReducer, initialState, applyMiddleware(thunk));
} This instance would then be able to |
Well, I think that's a problem. Think about it this way: If My two cents, 🌷 |
@unional, maybe I'm misunderstanding something, but let me try to give you a different perspective. The export interface Store<S> extends ReduxStore<S> {
dispatch: Dispatch<S>;
} in my PR and consequently the interface has to be imported from |
I can understand what is your confusion. If it is written in typescript, you would do module augmentation. And the If it follows your implementation, then other packages which augment |
From whichever is first in the middleware chain. After all the signature of this |
IMO is not a proper picture. Consider these: import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import prom from 'redux-promise';
import rootReducer from './reducers';
import { Store } from '???'
// Note: this API requires redux@>=3.1.0
const store: Store = createStore(
rootReducer,
applyMiddleware(thunk, prom)
);
const store2: Store = createStore(
rootReducer,
applyMiddleware(prom, thunk)
); Where should the |
I see your point. But how can you write a middleware with augmentation then and solve this issue? |
I am using the latest ts and still same issue of:
as I am dispatching a thunk any ideas? |
With module augmentation (as it is right now), you will get the |
@born2net I think you did not have the typings loaded. It is not distributed in 2.1 |
you have a link to the latest / proper definition file I should use? |
tx!!!!! |
unfortunately overriding still results with same error:
|
Can you give a sample of how are you using it? |
sure...
|
How do you get load the typings into your project? Are you using |
FYI if I put :any it fixes it...
|
I did not install any typings and just using my global typings.d.ts (which works great for all my other typings stuff) and pasted your code in... is that wrong? |
even after installing, commenting out and putting updated one, same issue
|
Nope, that most likely would not work. Your |
ok ya tried other way (see prev msg) ... same :/ |
Then in your |
ok I will be back in one hour and will try it ASAP thanks again for the support |
tried to install and got:
|
Forgot that this repo doesn't have |
Ar, its webstorm. Try to see if it compiles ok or not. It should be. At this point, it is webstorm issue. |
I understand... I appreciate the time... |
not sure if u r into ng2, but if u r: Sean |
param please follow my setup: https://github.com/born2net/Angular-kitchen-sink |
Released v2.2.0 with TypeScript definitions included. |
I would expect that Thunk would not overwrite the Redux API without also being backwards compatible. However, this is not the case. Instead, here's a workaround for Middleware type incompatibility found to be working in Fair warning: There's a possibility this is not intended behavior for the TypeScript compiler. import * as Redux from "redux";
import * as ReduxThunk from "redux-thunk";
const middleware: Redux.Middleware =
(api: Redux.MiddlewareAPI<void>) => (next: Redux.Dispatch<void>) => <A extends Action>(action: A) => {
return next(action);
}; @JackSkylark figured this one out. |
Hello! I worked with @rodolfo2488 on a fix for this. We believe that this pull request would fix the issue. |
I think the original issue can be resolved with improved typings based on reduxjs/redux#2563 which hopefully lands soon. |
The current implementation of the typings makes it impossible to create a middleware that only handles standard actions. This means a middleware must handle thunked actions even though it never actually handles them during runtime. (see issue and examples in reduxjs#82) By declaring the Dispatch and Store interfaces again instead of overloading the original implementation of redux a consumer of redux-thunk can choose the right implementation for his use case (e.g. ActionCreator vs. Middleware). fixes reduxjs#82
The overloading of
Dispatch
byredux-thunk
breaks middlewares that only handle standard action objects (as apposed to thunks).Example:
The resulting error is:
A possible fix to remove the compiler error would be to declare a union type for the Action, i.e.
Action | Redux.ThunkAction<any, IAppState, any>
:But this is incorrect if your middleware only handles
Action
!The better solution would be to declare a
Dispatch
interface within theredux-thunk
module that extendsRedux.Dispatch
. ThisDispatch
could then be used in action creators that return aThunkAction
:Middlewares on the other hand would use
Redux.Dispatch
(see middleware example above).The text was updated successfully, but these errors were encountered: