I struggled to come up with a meaningful title, but the example should make it really clear what I want:
interface Animal {
legs: number;
name: string;
color: string;
}
function animalBuilder(defaults: Partial<Animal>) {
return (rest: Partial<Animal>): Animal => ({...defaults, ...rest});
}
const builder = animalBuilder({color: 'black'});
builder({name: 'bear', legs: 4}); // Should error without 'name' and 'legs'
Is this possible? I know it would require a generic, but I'm not sure how to construct it. The defaults parameter should be a subset of Animal, and then rest would be all the properties that weren't provided in defaults.