-
Notifications
You must be signed in to change notification settings - Fork 201
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
Determine type of sidebar store automatically #4348
Conversation
2e4721d
to
db54a82
Compare
b9cc748
to
462663a
Compare
Codecov Report
@@ Coverage Diff @@
## master #4348 +/- ##
=======================================
Coverage 99.17% 99.17%
=======================================
Files 225 225
Lines 8588 8588
Branches 2030 2030
=======================================
Hits 8517 8517
Misses 71 71
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
db54a82
to
85d45b9
Compare
8907715
to
7120407
Compare
7120407
to
273f086
Compare
Avoid the need to manually create the `SidebarStore` type by inferring it automatically. This works as follows: 1. The `modules` argument to `createStore` has been converted to a tuple type (`[ModuleA, ModuleB, ...]`) 2. The type of a module that would result from merging all the individual modules (`ModuleA & ModuleB ...`) is computed using a `TupleToIntersection` utility type 3. `StoreFromModule` is used to compute the type of the store that the merged module would produce
273f086
to
c1f33ab
Compare
I'm not 100% certain that the trick used here to infer the sidebar store type is a good idea, but we can discuss, and we can easily revert back to the "manual" method of constructing the type if it causes us problems. |
@@ -149,6 +149,24 @@ function assignOnce(target, source) { | |||
return Object.assign(target, source); | |||
} | |||
|
|||
/** | |||
* @template T | |||
* @typedef {{[K in keyof T]: (x: T[K]) => void }} MapContravariant |
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 is part of a trick taken from the linked GitHub issue below. I can attempt to explain this, but ah, I don't expect understanding of it. Grokking what the TupleToIntersection
utility is used for is enough.
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.
OK, I think I get this enough to say it looks good :).
@@ -241,7 +260,7 @@ export function createStore(modules, initArgs = [], middleware = []) { | |||
} | |||
Object.assign(store, selectorMethods); | |||
|
|||
return store; | |||
return /** @type {any} */ (store); |
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.
Can you help me understand this any
?
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 is basically saying "trust me" to TS, since it can't figure out for itself that the object we've put together has the shape we promise.
Avoid the need to manually create the
SidebarStore
type by inferringit automatically. This works as follows:
modules
argument tocreateStore
has been converted to atuple type (
[ModuleA, ModuleB, ...]
)individual modules (
ModuleA & ModuleB ...
) is computedusing a
TupleToIntersection
utility typeStoreFromModule
is used to compute the type of the store that themerged module would produce
The merged module described in step 2 is not actually created when the code runs. It is a fiction that is used to "explain" to TS how the output of
createStore
depends on the input.