-
-
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
Incorrect typescript typings for middlewares #2740
Comments
I believe this is fixed in the 4.0 beta. I would try installing that and see if it fixes your issue. |
@timdorr issue is the same. Updated message to work with 4.0.0-beta.1: It looks like typings don't work nicely for middlewares because I can't use type of state in it, because it's type is S (see the comment in code): import * as redux from 'redux';
interface State {
app: {
title: string;
}
}
const initialState: State = {
app: {
title: 'awesome app',
}
}
function createMiddleware(): redux.Middleware {
return function<S, D>(api: redux.MiddlewareAPI<S, D>) {
return function (next: redux.Dispatch<D>): redux.Dispatch<D> {
return function <A extends D>(action: A): A {
const state = api.getState();
// error is shown here because `app` does not exist on type `S`
console.log('app title: ', state.app.title);
return next(action);
};
};
}
}
function appReducer(state: State, action) {
return state;
}
redux.createStore(
appReducer,
initialState,
redux.applyMiddleware(createMiddleware())
) My colleague advised me to extend redux.Middleware but then typings don't work either because following code should fail to compile, but it compiles just fine (see the comment in code): import * as redux from 'redux';
interface State {
app: {
title: string;
}
}
export interface Middleware extends redux.Middleware {
<S extends State, D = redux.Action>(api: redux.MiddlewareAPI<S, D>): (next: redux.Dispatch<D>) => redux.Dispatch<D>;
}
function createMiddleware(): Middleware {
return function<S, D>(api: redux.MiddlewareAPI<State>) {
return function (next: redux.Dispatch<D>): redux.Dispatch<D> {
return function <A extends D>(action: A): A {
const state = api.getState();
// `state` is of type `State2` which doesn't have `app` property, but typescript happily compiles this
console.log('app title: ', state.app.title);
return next(action);
};
};
}
}
interface State2 {
wtf: string;
}
const initialState2: State2 = {
wtf: 'ttf'
}
function appReducer(state: State2, action) {
return state;
}
redux.createStore(
appReducer,
initialState2,
redux.applyMiddleware(createMiddleware())
) environment:
so applyMiddleware should probably take generic parameter for state and pass it to the middlewares? // current definition
export function applyMiddleware(...middlewares: Middleware[]): GenericStoreEnhancer;
// should be probably defined something like:
export function applyMiddleware<S>(...middlewares: Middleware<S>[]): GenericStoreEnhancer; |
Just checking in to say that I'm going to take on this and other TypeScript typings issues for v4.0 during the next week. Please don't release yet 😅 |
We can always do a 4.0.1 to bump up typings. Now that we're on TS 2.x, things should be smoother. |
Note: I'm not planning on making 4.0 final any time soon. |
Would it be possible to give it another beta bump to push the latest changes in? #2773 has some really neat additions I'd like to try out (Middleware is generic!) 👍 |
@NathanNZ Just pushed 4.0.0-beta.2 |
Hi. It looks like typings don't work nicely for middlewares because I can't use type of state in it, because it's type is
S
(see the comment in code):My colleague advised me to extend
redux.Middleware
but then typings don't work either because following code should fail to compile, but it compiles just fine (see the comment in code):environment:
so
applyMiddleware
should probably take generic parameter for state and pass it to the middlewares?The text was updated successfully, but these errors were encountered: