I have a situation where I have a generic function that takes a type param and then calls another more specific function based on that type. I pass an optional parameter to the generic function that is required for some of the specific functions but not for all of them. Because the parameter is optional I get a typing error from trying to call a specific function where the parameter is required with something that could be undefined. Code below or stackblitz here. Is there a way I can type this properly so that it passes strictNullChecks?
function specificAction1(): number {
return 1;
}
function specificAction2(param2: boolean): number {
return 2;
}
function triggerGenericAction(
actionType: "action1" | "action2",
param2?: boolean
): number {
const specificActionForActionType = {
["action1"]: specificAction1,
["action2"]: specificAction2
};
return specificActionForActionType[actionType](param2);
}