-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Add state type to dispatch interface #1537
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
Changes from all commits
74e16aa
b0d60c4
2d29481
e767e13
658fa6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
import {Dispatch, Action} from "../../index.d.ts"; | ||
|
||
|
||
declare const dispatch: Dispatch; | ||
|
||
declare const dispatch: Dispatch<any>; | ||
|
||
const dispatchResult: Action = dispatch({type: 'TYPE'}); | ||
|
||
|
||
type Thunk<O> = () => O; | ||
// thunk | ||
declare module "../../index.d.ts" { | ||
export interface Dispatch<S> { | ||
<R>(asyncAction: (dispatch: Dispatch<S>, getState: () => S) => R): R; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ulfryk see dispatch's augmentation here that uses state type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to augment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can see from microsoft/TypeScript#6213 external module augmentation should work by doing // redux-thunk/index.d.ts
declare module "redux" {
export interface Dispatch<S> {
// ...
}
} @Igorbek could you please check this? If it works, I think this PR is good to merge. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Igorbek Sorry, I just realized that this is actually not ambient but external module augmentation introduced in TS 1.8 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're absolutely right, that was my intent to define additional dispatch signature through module augmentation for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks good to me :) |
||
|
||
const dispatchThunkResult: number = dispatch(() => 42); | ||
const dispatchedTimerId: number = dispatch(d => setTimeout(() => d({type: 'TYPE'}), 1000)); |
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.
Here
Dispatch<S>
is defined as a paramtrized type (generic) but the time parameter from signature is not used. @Igorbek can you explain the purpose?Also I think that after a rather long discussion we have decided to drop type parameters from
<A extends Action>(action: A): A;
due to middleware issues.See my initial proposition -> https://gist.github.com/ulfryk/69981ccfb488647b6ee3
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.
Having state type parameter
S
allows middlewares be more accurate in typings. For example, thunk function takes a dispatch and agetState(): S
, so withoutS
it couldn't be well-typed. See the example of this here below https://github.com/Igorbek/redux/blob/ts-def-improv/test/typescript/dispatch.ts#L9-L16Regarding second question, I think this signature express exactly what initial (without midllewares) dispatch does. It take an action and returns it. If I used less generic signature
(action: Action) => Action
, due to TypeScript rules it would fail on object literals, such asdispatch({ type: 'ADD_TODO', text: 'todo' })
(Object literals may only specify known properties). Moreover, it returns more precise result - typed action that was passed.