Skip to content
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

Fix TSDoc comments #3560

Merged
merged 1 commit into from
Sep 5, 2019
Merged

Fix TSDoc comments #3560

merged 1 commit into from
Sep 5, 2019

Conversation

jednano
Copy link
Contributor

@jednano jednano commented Sep 2, 2019

No description provided.

Copy link
Contributor Author

@jednano jednano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope these comments help you understand what's going on in this PR.

src/applyMiddleware.ts Outdated Show resolved Hide resolved
@@ -257,11 +257,7 @@ export default function createStore<S, A extends Action, Ext, StateExt>(
throw new Error('Expected the nextReducer to be a function.')
}

// TODO: do this more elegantly
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See line 84 for the "more elegant" approach, which allows any reducer to be assigned to currentReducer. It's all that can be done, really.

@@ -51,16 +51,11 @@ describe('applyMiddleware', () => {
const spy = jest.fn()
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos)

// the typing for redux-thunk is super complex, so we will use an as unknown hack
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why I installed redux-thunk as a dev dependency. I think it makes sense to ensure its compatibility here, seeing as the tests are using thunks.

@@ -51,16 +51,11 @@ describe('applyMiddleware', () => {
const spy = jest.fn()
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This store was not being typed properly, because the thunk wasn't typed properly. That's now fixed in this PR.

return dispatchedValue.then(() => {
expect(spy.mock.calls.length).toEqual(2)
})
await store.dispatch(addTodoAsync('Use Redux'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the store is typed properly, this dispatch call has no issues. The root of the problem has been fixed.

@@ -505,6 +505,7 @@ describe('createStore', () => {

it('throws if action type is missing', () => {
const store = createStore(reducers.todos)
// @ts-ignore
expect(() => store.dispatch({})).toThrow(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeScript won't let you call store.dispatch with a missing action type, now that the store is typed properly; thus, we need to // @ts-ignore this line to allow the test to run w/o compilation errors.

@@ -519,10 +520,10 @@ describe('createStore', () => {

it('does not throw if action type is falsy', () => {
const store = createStore(reducers.todos)
expect(() => store.dispatch({ type: false })).not.toThrow()
expect(() => store.dispatch({ type: 0 })).not.toThrow()
expect(() => store.dispatch({ type: false } as any)).not.toThrow()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, because the store is properly typed, TS won't compile this, so I'm just casting as any to allow it to compile. Pretty common to see as any usage in tests. In fact, I usually change my .eslintrc to allow any in test files.


export function thunk({ dispatch, getState }: MiddlewareAPI) {
return (next: Dispatch) => <T>(action: ThunkAction) =>
export const thunk: Middleware<
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, using ThunkMiddleware kept returning a type of any for some reason, so I resorted to this redux Middleware type instead, which gives us a properly-typed thunk. Worth looking into in the redux-thunk library why this is happening, but outside the scope of this PR.

@timdorr
Copy link
Member

timdorr commented Sep 3, 2019

We're still in the middle of the conversion. We want to:

  1. Move all the types into the source verbatim.
  2. Get things building and outputting essentially the same as before. <- We are here
  3. Make improvements to the types.

So, this PR is going to conflict for a bit while we get finished with step 2. I'd hold off until #3536 closes, at which point we'll be ready for improvements.

@jednano jednano force-pushed the tsdoc-types branch 4 times, most recently from b684953 to 180e93e Compare September 3, 2019 19:57
@jednano jednano changed the title Improve TypeScript types Fix TSDoc comments Sep 3, 2019
@jednano
Copy link
Contributor Author

jednano commented Sep 3, 2019

@timdorr I've limited this PR in scope to just TSDoc comments. I'll hold off for the other stuff.

@timdorr timdorr merged commit 9e71b4c into reduxjs:ts-conversion Sep 5, 2019
@jednano jednano deleted the tsdoc-types branch September 5, 2019 18:11
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this pull request Aug 21, 2021
Former-commit-id: 48de485
Former-commit-id: 150b5f7
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this pull request Aug 21, 2021
Former-commit-id: 48de485
Former-commit-id: 150b5f7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants