I am trying to make type handlers for decux actions based on an action type. For example, any action can be described as:
type ActionType<T extends string> = {
type: T
};
For a specific action, you can:
type MyAction = ActionType<'hey' | 'there'>;
Now I would like to limit the function of the handler to allow only “hey” or “there” as a type. Finally, I expect something like this:
handleAction<MyAction>('hey');
where the definition of the handler function can be:
function handleAction<A extends ActionType<T>>(type: T){
...
}
But I have a typescript compiler error:
TS2304: Cannot find the name "T".
So, I need to change this definition of the handler function as follows:
function handleAction<A extends ActionType<T>, T extends string>(type: T){
...
}
This works, but it looks very ugly:
handleAction<MyAction, 'hey' | 'there'>('hey');
TS Playground
What is the best way to handle this?
source
share