In the code snippet provided, I am facing a challenge where I need to pass data to an if block with two different types. These types are handled separately in the if block. How can I make TypeScript understand that the selected object could be either of type CustomCar or CustomPlane? I want to avoid having separate actionGuard functions for each type and instead have one function that can handle both cases simultaneously. Currently, when I try to use the selected object in the if condition, TypeScript throws an error saying that selected could be either type, each with different keys and values, all without having to resort to casting.
import { CustomCar } from '../app/carSlice';
import { CustomPlane } from '../app/planeSlice';
export const actionGuard = (
selected: CustomCar | CustomPlane
check: CustomCar[] | CustomPlane[]
term: string,
dispatch: any,
SetSnackBarMsg: any,
action: string
) => {
if (term === 'CustomCar' ) {
if (array.includes((selected as CustomCar){
// some logic
}
if (term === 'CustomPlane') {
if (array.includes((selected as CustomPlane ){
// some logic
}
};