I'm using reducers in React using useReducer
. I have several actions whose correct types require different generic parameters. However, I have to export a single Action type to use with the dispatch
method everywhere. Taking a union of two types with different generic parameters does not seem to work:
type MyTable1 = {
"a": "b",
"c": "d"
}
type MyTable2 = {
"e": "f",
"g": "h"
}
type Action1<K extends keyof MyTable1> = {"type": "action1"; "val": MyTable1[K]}
type Action2<K extends keyof MyTable2> = {"type": "action1"; "val": MyTable2[K]}
type AllActions = Action1 | Action2;
Is it possible for this to work somehow?
My current workaround is to have dedicated methods with correct typings for creating the action types that need generic parameters. However, I still end up exporting the more weakly-typed action definitions, so it's possible for a client to dispatch
a bad action.