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
import{Action,Config,IContext}from'overmind';import{Setting,Settings}from'./state';exportconstset=<TextendsSetting>({state}: IContext<Config>,{key, value}: {key: T;value: Settings[T]}): void=>{state.settings={...state.settings,[key]: value};};exportconsttest: Action=({actions})=>{// should not compile but does// value takes type: 'dark' | 'light' | boolean;actions.set({key: Setting.Theme,value: true})// is correctactions.set({key: Setting.HideHeader,value: true})// should not compile but doesactions.set({key: Setting.HideHeader,value: 'dark'})};
With some workarounds it is possible to get it working but it doesn't look really good:
actions.ts@workaround
import{Action,Config,IContext}from'overmind';import{Setting,Settings}from'./state';// When returning a method with a generic type it does work, but we have to execute the state change inside an action therefore// we need runInActionexportconstset=({actions}: IContext<Config>,_: void)=><TextendsSetting>({key, value}: {key: T;value: Settings[T]}): void=>{actions.settings.runInAction(({state})=>{state.settings={...state.settings,[key]: value};})};exportconstrunInAction: Action<(ctx: IContext<Config>)=>void>=(context,callback)=>callback(context);exportconsttest: Action=({actions})=>{// does not compile: true is not assignable to 'dark' | 'light'actions.set()({key: Setting.Theme,value: true})// is correctactions.set()({key: Setting.HideHeader,value: true})// does not compileactions.set()({key: Setting.HideHeader,value: 'dark'})};
Is there a better way to do this? We have an application that is heavily dependent on this pattern thus a more optimal solution would be awesome.
The text was updated successfully, but these errors were encountered:
The documentation is updated with a different default official typing pattern now, which supports generics. Let me know if you need any more information: https://overmindjs.org/core/typescript
I want to use generics in overmind actions, but this doesn't work correctly because the actions will be resolved without the generics.
Here an example.
state.ts
actions.ts
With some workarounds it is possible to get it working but it doesn't look really good:
actions.ts@workaround
Is there a better way to do this? We have an application that is heavily dependent on this pattern thus a more optimal solution would be awesome.
The text was updated successfully, but these errors were encountered: