Is there a way to modify the addSuffix function to handle two different types and return them simultaneously?
Here's an example:
type First = {
name: string,
address: string,
}
type Second = {
name: string,
email: string,
}
type Third = First | Second;
function addSuffix(payload: Third): Third {
payload.name += "suffix";
return payload;
}
const a: First = {
"name": "John",
"address": "London",
}
const b: Second = {
"name": "Alice",
"email": "example@example.com"
}
function doA(payload: First): First {
// some operations
return addSuffix(payload);
}
function doB(payload: Second): Second {
// some operations
return addSuffix(payload);
}
doA(a);
doB(b);
An issue arises:
'Third' is not compatible with 'First'. 'address' is required in 'First' but missing in 'Second'.
How can the addSuffix function be modified to resolve this issue? In traditional OOP languages, interfaces can be used (interface with name property), but how can it be achieved in TypeScript?