-
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
Add TypeScript definitions #77
Conversation
Looks good to me. Does it compile well? |
@Igorbek It does and it infers types correctly, e.g. if I change declare const store: Store<{foo: string}>;
store.dispatch((dispatch, getState) => {
const state = getState();
const foo: string = state.foo;
}); |
Could you also export the And wouldn't it be better if you also added a second type parameter for the extraArgument ? This way, we could write something like function actionCreator(): ThunkAction<Api> {
return async (dispatch, getState, api) => {
// Our code here with complete inference
}
} I know I removed the state from the typings, but it could be added back with overloads. |
@JabX I'll do it, but I need some clarification. To be able to annotate type ThunkAction<E> = <S, R>(dispatch: Dispatch<S>,
getState?: () => S,
extraArgument?: E) => R; But now there's no way compiler could infer State type inside of The most generic version would be: type ThunkAction<S, E, R> = (dispatch: Dispatch<S>,
getState?: () => S,
extraArgument?: E) => R;
declare module "redux" {
export interface Dispatch<S> {
<E, R>(asyncAction: ThunkAction<S, E, R>): R;
}
} Now you can add your specific action type by narrowing type MyThunkAction<R> = ThunkAction<MyState, Api, R>; What do you say? |
That's what I'd like to have, yeah. But maybe we could have some overloads if other people want to specify some types but not all because they don't use them. Maybe something like type ThunkAction<R> = (dispatch: Dispatch<any>) => R
type ThunkAction<R, S> = (dispatch: Dispatch<S>, getState?: () => S) => R
type ThunkAction<R, S, E> = (dispatch: Dispatch<S>, getState?: () => S, extraArgument?: E) => R (I'm not sure about removing the arguments when removing types). By the way, why do we need a |
It's not possible to overload generic types, only call signatures.
|
Oh, I thought we could, my mistake, I should have checked before. |
The following signature/type makes much more sense to me: type ThunkAction<R, S, E> = (dispatch: Dispatch<S>, getState: () => S, extraArgument: E) => R; Please note that both const tunkAction: ThunkAction<void /* have no return data */,
{} /* don't care about state type*/,
{} /* don't care about extra argument*/> = dispatch => {
dispatch(a1());
dispatch(a2());
}; |
This style of augmentation works like global monkey patching. Even if I just require |
Yes, that's correct, but there's absolutely no way of doing otherwise. It would require some kind of special support from Typescript for this scenario, which is terribly specific (well, there are other librairies which work like that (hello express), but still). |
Any chance of a merge? |
@ulfryk Could you please review the latest version and merge if everything's ok? |
@aikoven - IMO it looks great! :) |
Do you see the need of type |
|
||
declare module "redux" { | ||
export interface Dispatch<S> { | ||
<R, E>(asyncAction: ThunkAction<R, S, E>): R; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@unional S
is used here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but does it really help?
It can simply be:
type ThunkAction<R, S, E> = (dispatch: Dispatch, getState: () => S, extraArgument: E) => R;
declare module 'redux' {
export interface Dispatch {
<R, S, E>(asyncAction: ThunkAction<R, S, E>): R;
}
}
The point is the Dispatch
does not gain anything by having the type S
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course, it does. Your Dispatch
is tied with your store which is typed by state type. With your signature one could redefine the state type, which would not be allowed in reality. I have the same concern with E
extra argument type, but cannot be well defined with current design. I've been experimenting with some new approach that could address this issue as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. That make sense.
I'm experimenting alternative approach that Store
is not typed with <S>
. Only reduce
and getState
is typed with <S>
because the producer (which reduce
) and consumer (which getState
) have separate concerns.
I am trying to use this, and I noticed that the npm version of this package still (as of today) does not include the index.d.ts file. Version numbers are the same (2.1.0) and index.d.ts was checked in three months ago, but it doesn't get downloaded on npm install. I tried getting the index.d.ts file from DefinitelyTyped, but that is different in that it wraps the |
@jimsugg 2.1.0 was released on 10th May 2016, the PR got merged on 23 June. |
Yes, I noticed that. I am not familiar enough with npm publishing to know for sure why the npm build didn't get updated. I suspect you have to bump the version number, but there may be more to it than that. I solved my problem by pointing typings directly at the committed file in the github repo here. It would definitely be much better to get the npm build updated. |
It wouldn't be so bad if the one in DefinitelyTyped wasn't a problematic file. That one wraps the new interface and type in a namespace and then doesn't export them from that. It would be good to get that file updated with a good one as well. |
Understood - that's what I suspected. Just wanted to point out the issues while they were fresh in my mind. Thanks! |
tried all the combinations and still getting:
regards Sean |
Released v2.1.1 with TypeScript definitions included. |
Thank you for your work and thank you for adding better TypeScript Support to redux-thunk. Sadly, for me the switch from 2.1.0 to 2.1.1 broke the build. Error message:
This was my fault because I defined the redux-thunk version as |
@dmies This is strange, where did your old typings come from? AFAIK, bundled typings have lower priority than local or installed from |
@aikoven I had no other redux-thunk typings installed (tsconfig with |
Ok, sorry, that's not good at all. I'll make another patch release with typings removed, and add them with minor release. |
Done: v2.2.0 |
Any idea how to describe ThunkActions that can dispatch more than one type of action? |
Cc @Igorbek, @use-strict, @Pajn