You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Originally posted by no-stack-dub-sack November 18, 2021
Hello, the current use-gloabl-hook documentation on working with TypeScript shows the following for defining action types:
typeMyAssociatedActions={setValue: (value: string)=>void;otherAction: (other: boolean)=>void;};// setValue will be returned by globalHook as setValue.bind(null, store)// This is one reason we have to declare a separate associated actions typeconstsetValue=(store: Store<MyState,MyAssociatedActions>,value: string)=>{store.setState({ ...store.state, value });store.actions.otherAction(true);};constotherAction=(store: Store<MyState,MyAssociatedActions>,other: boolean)=>{/* cool stuff */};
This is problematic for applications with large numbers of actions (as noted by the comment in the example). Since the store argument is bound to the action function by the library, to represent your actions in the way that the library understands, you need to hand write the type for each action by omitting the first argument (as shown above), or come up with some other more elegant way to handle it so that you don't have to do this.
I'm proposing that a utility type be added to the type definitions for the library so that there's a consistent and easy way to handle this issue. This is just a quick sketch, but I think it can be accomplished by adding three types (inspired by TS's OmitThisParameter<T> utility type):
typeStoreParameterType<T>=Textends(store: infer U,
...args: any[])=>void|Promise<void>
? U
: unknown;typeOmitStoreParameter<T>=Textends(store: StoreParameterType<T>,
...args: infer A)=> infer R
? (...args: A)=>R
: T;typeUseGlobalActionTypes<Textends{[key: string]: (...args: any[])=>any},>={[KinkeyofT]: OmitStoreParameter<T[K]>;};
Now, with the same actions as shown in the example above and in the docs, you can simply do:
constactions={
setValue,
otherAction,}typeRootActionTypes=UseGlobalActionTypes<typeofactions>;// type RootActionTypes = {// setValue: (value: string) => void;// otherAction: (other: boolean) => void;// }// ^^ same exact type created manually in the documentation example
This would make typing large numbers of actions much simpler and super easy. Please let me know what you think. I'd be happy to put together a PR and discuss further.
The text was updated successfully, but these errors were encountered:
Discussed in #57260
Originally posted by no-stack-dub-sack November 18, 2021
Hello, the current use-gloabl-hook documentation on working with TypeScript shows the following for defining action types:
This is problematic for applications with large numbers of actions (as noted by the comment in the example). Since the
store
argument is bound to the action function by the library, to represent your actions in the way that the library understands, you need to hand write the type for each action by omitting the first argument (as shown above), or come up with some other more elegant way to handle it so that you don't have to do this.I'm proposing that a utility type be added to the type definitions for the library so that there's a consistent and easy way to handle this issue. This is just a quick sketch, but I think it can be accomplished by adding three types (inspired by TS's
OmitThisParameter<T>
utility type):Now, with the same actions as shown in the example above and in the docs, you can simply do:
This would make typing large numbers of actions much simpler and super easy. Please let me know what you think. I'd be happy to put together a PR and discuss further.
The text was updated successfully, but these errors were encountered: