You need to combine both of types instead of union usage.
itemClicked: Subject<MenuTab, MenuItem> & Subject<MenuLaunchTab>;
now itemClicked
supports both Subject<MenuTab, MenuItem>
and Subject<MenuLaunchTab>
but neither Subject<MenuTab>
nor Subject<MenuLaunchTab, MenuItem>
.
A function should unite all possible types of its overrides.
Unfortunately, it means there's no other way than
function(tab: MenuTab | MenuLaunchTab, item?: MenuItem)
What you can do is to detect types at the beginning and then split your flow.
function(tab: MenuTab | MenuLaunchTab, item?: MenuItem) {
// or other check that fits, because their definition is unclear.
const menuLaunchTab: MenuLaunchTab | undefined = item ? undefined : tab;
const menuTab: MenuTab | undefined = item ? tab : undefined;
if (menuTab) {
handlerOfmenuTab(menuTab, item);
} else if (menuLaunchTab) {
handlerOfmenuLaunchTab(menuLaunchTab);
}
}