My preference in animals
type Animal = {
name: string;
age: number;
dimensions: number[];
};
Now I aim to create a function that allows me to input an object with any combination of those fields, but strictly only those fields and with the correct type. For example
type OnlyAnimalProps = Record<string, any>; // not recommended
function createAnimal(parent: Animal, props: OnlyAnimalProps) {
return {...parent, ...props};
}
These examples should work fine
g1 = createAnimal(someAnimal, {}); // okay
g2 = createAnimal(someAnimal, {name: "Cindy"}); // okay
g3 = createAnimal(someAnimal, {age: 12, dimensions: [1, 3, 2]}); // okay
However, these examples should not work
e1 = createAnimal(someAnimal, {height: 123}); // wrong, "height" property doesn't exist
e2 = createAnimal(someAnimal, {name: 456}); // wrong, name should be a string
I understand this solution may already exist, but my searches have not yielded results