-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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(types): getState is not bound to MiddlewareAPI #4737
Conversation
Motivation: The `getState` function provided by `MiddlewareAPI` is not bound to any object, and as such it can be used freely as in the examples. The `MiddlewareAPI` type does not make this explicit, and typescript linters can report this as problematic usage.
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
Co-authored-by: Ben Durrant <[email protected]>
This should have some sort of test to go along with it. That will prevent a regression in the future. |
I'm able to create a test if we revert to export interface MiddlewareAPI<D extends Dispatch = Dispatch, S = any> {
dispatch: D
getState(this: void): S
} however, with the current signature ( |
Never mind -- found something that covers the bases, I think. |
test/typescript/middleware.test-d.ts
Outdated
const customMiddleware: Middleware<{}, State> = api => next => action => { | ||
const { getState } = api | ||
|
||
expectTypeOf(getState).thisParameter.toEqualTypeOf() |
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.
this doesn't actually fail if the signature is changed back
if we really need a test, then we can switch back to the this: void method, but i'm not convinced that we should worry too much about testing a change solely to satisfy a lint rule, and I think this: void is unnecessary noise for a codebase that doesn't actually use the lint rule it's meant to satisfy
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 swear I tested this.. thanks for double checking it.
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.
So, to proceed: remove the test and keep the current signature?
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.
that would be my preference - @timdorr?
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 would add the test as one commit, which should fail, and then add the fix in the next commit to show that this satisfies the condition we're looking to correct.
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.
Oh, I see what you're saying. Sorry, I was thinking about the actual test passing.
As long as the test essentially functions as a lint check, I think that's sufficient. Can we get a spot check by importing this version into a project and seeing if that does pass the lint (vs the current release failing)?
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've tested it out in my project and it fixes the lint violation.
Should I remove the test?
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.
That seems good enough to me. Leaving the test seems fine, since it at least validates the behavior and will trip if someone breaks this again later on.
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.
the test doesn't actually verify anything, and will not break if anything is changed.
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.
It's an eslint rule fix. Even if this breaks, it will not cause any bugs in any production environment but only give someone red squigglies. I don't think it needs a test honestly.
Thanks! |
PR Type
Does this PR add a new feature, or fix a bug?
Fixes a type bug
Why should this PR be included?
The
getState
function provided byMiddlewareAPI
is not bound to any object, and as such it can be used freely as in the examples.The
MiddlewareAPI
type does not make this explicit, and typescript linters can report this as problematic usage.Checklist
docs/usage/deriving-data-selectors.md
Bug Fixes
What is the current behavior, and the steps to reproduce the issue?
What is the expected behavior?
getState
can be used with the linting rule enabled and not produce errorsHow does this PR fix the problem?
Fixes the signature of
getState