-
Notifications
You must be signed in to change notification settings - Fork 4.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
Autocompletion support for useSelect (via jsDoc annotations) #41596
Changes from all commits
69c44f0
12b4db5
cd06866
e43aa5b
767d0ed
ee9724b
39860fb
ae2b8c8
81bba65
e8c1e5d
dc2093c
a19b263
1e25516
a295484
a9b9d31
88265ab
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 |
---|---|---|
|
@@ -27,7 +27,7 @@ export interface StoreDescriptor< Config extends AnyConfig > { | |
export interface ReduxStoreConfig< | ||
State, | ||
ActionCreators extends MapOf< ActionCreator >, | ||
Selectors extends MapOf< Selector > | ||
Selectors | ||
> { | ||
initialState?: State; | ||
reducer: ( state: any, action: any ) => any; | ||
|
@@ -37,6 +37,40 @@ export interface ReduxStoreConfig< | |
controls?: MapOf< Function >; | ||
} | ||
|
||
export type UseSelectReturn< F extends MapSelect | StoreDescriptor< any > > = | ||
F extends MapSelect | ||
? ReturnType< F > | ||
: F extends StoreDescriptor< any > | ||
? CurriedSelectorsOf< F > | ||
: never; | ||
|
||
export type MapSelect = ( select: SelectFunction ) => any; | ||
|
||
export type SelectFunction = < S >( store: S ) => CurriedSelectorsOf< S >; | ||
|
||
export type CurriedSelectorsOf< S > = S extends StoreDescriptor< | ||
ReduxStoreConfig< any, any, infer Selectors > | ||
> | ||
? { [ key in keyof Selectors ]: CurriedState< Selectors[ key ] > } | ||
: never; | ||
|
||
/** | ||
* Removes the first argument from a function | ||
* | ||
* This is designed to remove the `state` parameter from | ||
* registered selectors since that argument is supplied | ||
* by the editor when calling `select(…)`. | ||
* | ||
* For functions with no arguments, which some selectors | ||
* are free to define, returns the original function. | ||
*/ | ||
export type CurriedState< F > = F extends ( | ||
state: any, | ||
...args: infer P | ||
) => infer R | ||
? ( ...args: P ) => R | ||
: F; | ||
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. what was the issue with this inference? is it that we lose the generic type parameters through this inference? so if we had 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. More or less:
The only solution I found is quite convoluted so I went for shipping the simple Curry type in this PR. We can always ship an updated version in a follow-up PR. |
||
|
||
export interface DataRegistry { | ||
register: ( store: StoreDescriptor< any > ) => void; | ||
} | ||
|
@@ -51,11 +85,10 @@ export interface DataEmitter { | |
|
||
// Type Helpers. | ||
|
||
type ActionCreatorsOf< | ||
Config extends AnyConfig | ||
> = Config extends ReduxStoreConfig< any, infer ActionCreators, any > | ||
? { [ name in keyof ActionCreators ]: Function | Generator } | ||
: never; | ||
type ActionCreatorsOf< Config extends AnyConfig > = | ||
Config extends ReduxStoreConfig< any, infer ActionCreators, any > | ||
? { [ name in keyof ActionCreators ]: Function | Generator } | ||
: never; | ||
|
||
type SelectorsOf< Config extends AnyConfig > = Config extends ReduxStoreConfig< | ||
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.
unfortunately until we get hyperlinked or better-resolved types I feel like this is just as useless as
Function
before it. not a knock against this PR - nothing I think we can do about it now. maybe it's a little better since you can search forUseSelectReturn
in the code…