I have the following code where I want to pass data to an if block with 2 different types that are tackled in if block how to make type script know that the (selected object) is customary and in the other scenario custom plane since I am passing 2 different types and I don't want to do 2 different actionGuard functions but one that can handle both cases at once, in other words, the selected object is erroring out in the if condition saying that selected could be this or this a both are different in keys and values and without having to use 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
}
};