I have Person, Address and PersonWithAddress types (a person with an an address property of type Address)
I am trying to come up with a Type-safe way to move from a Person to a PersonWithAddress once the relevant property has been set, so my function can then return a PersonWithAddress that is guaranteed to have an address.
Is there any way of achieving this in a Type-safe way?
type Person = { name: string };
type PersonWithAddress = Person & { address: Address };
type Address = { text: string };
function getPersonWithAddress(): PersonWithAddress {
const person: Person = { name: 'Bob' };
const address: Address = { text: "Somewhere" };
person.address = address;
return person;
}```