-
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
Make typings compatible with Redux 4.0.0 #180
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6776450
Upgrade redux to 4.0.0-beta.2
086802a
Make TypeScript definition support 4.0.0-beta.2
9d5899c
Upgrade TypeScript to ~2.4.0, Fix test cases
b9d9000
Update type definition, Fix test cases
4eb7404
Remove unnecessary default values for some generic types
196df61
WIP: Refine test cases
f86bd3f
Fix typings and test cases
01df0b0
update redux to 4.0.0, fix test cases
b595343
export ThunkDispatch
edcadd1
Remove this Code settings file.
timdorr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
import {Middleware, Dispatch} from "redux"; | ||
import { Middleware, Action, AnyAction } from "redux"; | ||
|
||
|
||
export type ThunkAction<R, S, E> = (dispatch: Dispatch<S>, getState: () => S, | ||
extraArgument: E) => R; | ||
export type ThunkAction<R, S, E, A extends Action> = ( | ||
dispatch: ThunkDispatch<S, E, A>, | ||
getState: () => S, | ||
extraArgument: E | ||
) => R; | ||
|
||
declare module "redux" { | ||
export interface Dispatch<S> { | ||
<R, E>(asyncAction: ThunkAction<R, S, E>): R; | ||
} | ||
interface ThunkDispatch<S, E, A extends Action> { | ||
<T extends A>(action: T): T; | ||
} | ||
|
||
interface ThunkDispatch<S, E, A extends Action> { | ||
<R>(asyncAction: ThunkAction<R, S, E, A>): R; | ||
} | ||
|
||
type ThunkMiddleware<E, S = {}, A extends Action = AnyAction> = Middleware<ThunkDispatch<S, E, A>, S, ThunkDispatch<S, E, A>>; | ||
|
||
declare const thunk: Middleware & { | ||
withExtraArgument(extraArgument: any): Middleware; | ||
declare const thunk: ThunkMiddleware<undefined> & { | ||
withExtraArgument<E>(extraArgument: E): ThunkMiddleware<E> | ||
}; | ||
|
||
export default thunk; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,52 @@ | ||
import {Store, Middleware} from 'redux'; | ||
import thunk, {ThunkAction} from '../index.d.ts'; | ||
import { createStore, applyMiddleware } from 'redux'; | ||
import thunk, { ThunkAction } from '../index'; | ||
|
||
type State = { | ||
foo: string; | ||
}; | ||
|
||
type Actions = { type: 'FOO' }; | ||
|
||
declare const store: Store<{foo: string}>; | ||
function fakeReducer(state: State, action: Actions): State { | ||
return state; | ||
} | ||
|
||
const store = createStore(fakeReducer, applyMiddleware(thunk)); | ||
|
||
store.dispatch(dispatch => { | ||
dispatch({type: 'FOO'}); | ||
dispatch({ type: 'FOO' }); | ||
}); | ||
|
||
store.dispatch((dispatch, getState) => { | ||
const state = getState(); | ||
function testGetState(): ThunkAction<void, State, {}, Actions> { | ||
return (dispatch, getState) => { | ||
const state = getState(); | ||
const foo: string = state.foo; | ||
}; | ||
} | ||
|
||
const foo: string = state.foo; | ||
}); | ||
store.dispatch(testGetState()); | ||
|
||
const middleware: Middleware = thunk.withExtraArgument('bar'); | ||
const storeThunkArg = createStore( | ||
fakeReducer, | ||
applyMiddleware(thunk.withExtraArgument('bar')) | ||
); | ||
|
||
store.dispatch((dispatch, getState, extraArg) => { | ||
storeThunkArg.dispatch((dispatch, getState, extraArg) => { | ||
const bar: string = extraArg; | ||
console.log(extraArg); | ||
}); | ||
|
||
const thunkAction: ThunkAction<void, {foo: string}, {bar: number}> = | ||
(dispatch, getState, extraArg) => { | ||
const foo: string = getState().foo; | ||
const bar: number = extraArg.bar; | ||
const thunkAction: ThunkAction<void, State, { bar: number }, Actions> = ( | ||
dispatch, | ||
getState, | ||
extraArg | ||
) => { | ||
const foo: string = getState().foo; | ||
const bar: number = extraArg.bar; | ||
|
||
dispatch({type: 'FOO'}); | ||
}; | ||
dispatch({ type: 'FOO' }); | ||
}; | ||
|
||
const thunkActionDispatchOnly: ThunkAction<void, {}, {}> = dispatch => { | ||
dispatch({type: 'FOO'}); | ||
const thunkActionDispatchOnly: ThunkAction<void, {}, {}, Actions> = dispatch => { | ||
dispatch({ type: 'FOO' }); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
E
could be inferred inwithExtraArgument
.S
could be inferred inMiddleware
. Now the problem isA
is not inferable. I'm thinking of opening a PR in https://github.com/reactjs/redux to pass action types toMiddleware
EDIT: I'm wrong I guess.