In TypeScript, 2.2...
Let\'s say I have a Person type:
interface Person {
name: string;
hometown: string;
nickname: string;
}
After a lot of digging, I think what I'm trying to do just isn't possible in TypeScript... yet. When spread/rest types land, I think it will be, though, with syntax something along the lines of { ...Person, nickname?: string }.
For now, I've gone with a more verbose approach, declaring the properties that are required:
type MakePersonInput = Partial & {
name: string;
hometown: string;
};
function makePerson(input: MakePersonInput): Person {
return {...input, nickname: input.nickname || input.name};
}
This unfortunately requires me to update MakePersonInput whenever I add more required properties to Person, but it's impossible to forget to do this, because it will cause a type error in makePerson.