-
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
Changes from all commits
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 |
---|---|---|
|
@@ -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 | ||
*/ | ||
|
||
/** | ||
* Utility that turns a tuple type `[A, B, C]` into an intersection `A & B & C`. | ||
* | ||
* The implementation is magic adapted from | ||
* https://github.com/microsoft/TypeScript/issues/28323. Roughly speaking it | ||
* works by computing a type that could be assigned to any position in the | ||
* tuple, which must be the intersection of all the tuple element types. | ||
* | ||
* @template T | ||
* @template {Record<number, unknown>} [Temp=MapContravariant<T>] | ||
* @typedef {Temp[number] extends (x: infer U) => unknown ? U : never} TupleToIntersection | ||
*/ | ||
|
||
/** | ||
* Create a Redux store from a set of _modules_. | ||
* | ||
|
@@ -173,10 +191,11 @@ function assignOnce(target, source) { | |
* `use-store.js`. This returns a proxy which enables UI components to observe | ||
* what store state a component depends upon and re-render when it changes. | ||
* | ||
* @param {Module<any,any,any,any>[]} modules | ||
* @template {readonly Module<any,any,any,any>[]} Modules | ||
* @param {Modules} modules | ||
* @param {any[]} [initArgs] - Arguments to pass to each state module's `initialState` function | ||
* @param {any[]} [middleware] - List of additional Redux middlewares to use | ||
* @return Store<any,any,any> | ||
* @return {StoreFromModule<TupleToIntersection<Modules>>} | ||
*/ | ||
export function createStore(modules, initArgs = [], middleware = []) { | ||
/** @type {Record<string, unknown>} */ | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Can you help me understand this 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. 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. |
||
} | ||
|
||
/** | ||
|
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.