I am trying to create a function whose parameter can be one of two types. The function then may invoke another function. I am, however, getting an error message. You can see the script and a screenshot below. How do I write this function so that it can accept a parameter with one of two types? Thanks!
interface A {
a: string;
title: string,
}
interface B {
b: number;
title: string;
}
const alpha = (data: A) => data.a;
const beta = (data: B) => data.b;
const foo = (data: A | B) => {
if (data.title === 'a') {
return alpha(data); // Message: "Argument type A | B is not assignable to type A"
}
if (data.title === 'b') {
return beta(data); // Message: "Argument type A | B is not assignable to type B"
}
};
