I've been struggling to create a versatile function that can return a specific interface based on an enum argument, but all my attempts have failed.
Could it be possible that I missed something or am simply approaching it the wrong way?
If I try to typecast the result to that interface, everything works fine, but it doesn't serve much purpose for me.
enum Operations {
Add = "add",
Subtract = "subtract",
}
interface BaseAction {
operation: Operations;
timestamp: number;
}
interface AddAction extends BaseAction {
operation: Operations.Add;
}
interface SubtractAction extends BaseAction {
operation: Operations.Subtract;
}
type GenericAction = AddAction | SubtractAction;
const createAction = (operation: Operations) => ({
operation,
timestamp: new Date().getTime(),
});
const executeAction = (action: GenericAction) => {};
const action = createAction(Operations.Add);
executeAction(action);