-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Typescript & Middleware extensions #1712
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
Comments
@aikoven, please give explicit instructions. I think I've put enough time into trying to fill in the blanks. I'm not making any progress. Given As proof of effort, let's start with the obvious:
import thunkMiddleware from 'redux-thunk';
console.log(thunkMiddleware) Result:
This one comes from
Aah, it'll only work if you use the global @HenrikBechmann offers, in DefinitelyTyped/DefinitelyTyped#6231:
Oh, that'll be easy to slot in, we lie to ourselves after the misadventures to date.
More misadventures ensue, their shape depending on the angle of the attempt. This one produces a mysteriously hidden middleware function: declare module "redux-thunk" {
import { Middleware } from 'redux'
export default Middleware
} This is better. Note I'm not so much programming by this point as word processing: declare module "redux-thunk" {
import { Middleware } from 'redux'
const thunkMiddleware: Middleware
export default thunkMiddleware
} My success that it compiles is short lived, as I then try to use the module: const thunkAction = getVersionFromServer(fetch);
store.dispatch(thunkAction); The compiler complains:
Aha! @aikoven said all we need to do, basically, is augment the interface! declare module 'redux' {
export interface Dispatch<S> {
<R>(thunkAction: (dispatch: Dispatch<S>) => R): R;
}
} That doesn't so much 'augment' it, as entirely mask the official typings:
There's hours more of this (anyone else best mates with const thunkAction = getVersionFromServer(fetch);
store.dispatch(thunkAction as any as { type: string }); Slow clap, everyone. Oh, the Java guys will totally leave us alone, now. You said it's basic, @aikoven. How about writing it down, eh? |
That is the definition of thunk's dispatch: declare module "redux" {
export interface Dispatch<S> {
<R>(asyncAction: (dispatch: Dispatch<S>, getState: () => S) => R): R;
}
} Let me try to reproduce your issues. |
@garthk I'm sorry if my words offended you. I'll add comments to the definition file with clarification on how new |
No worries, @aikoven: you weren't offensive. I'm sorry I got stroppy. I tried to edit it down, but must have failed. If you could document how to add new dispatch signatures, though, I'd find it very handy. |
So, this looks to be more on the side of the middleware libraries. It looks like redux-thunk got typings added. Other libraries that need typings should have issues/PRs opened up on their respective repos. Thanks for the investigation on this! |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I'm wondering how the typescript type definitions for extension via middleware are supposed to work (when extending the types of
Action
s that are possible).For instance, let's take the module redux-thunk (one of the simplest redux middlewares).
It allows you to dispatch a function with type:
(Dispatch, ()=>S) => void
, (that is, a function whose first parameter is the dispatch function, and whose second parameter is the getState function).suppose I have a trivial type definition for redux-thunk:
And I use it in my app to create a store like:
I can't dispatch a redux-thunk function:
This is a complex api to account for in a type system, since the signature of
dispatch
changes depending on what middleware is loaded:becomes:
(where
ThunkAction
is(Dispatch, ()=>S) => void
from above.I'm not really sure immediately how to address this, but I was just wondering if there is an intended way to work with redux middleware.
cc'ing @aikoven @Igorbek @use-strict
The text was updated successfully, but these errors were encountered: