Here is the data that I have:
const pets = [
{
type: 'cat',
name: 'penny'
},
{
type: 'dog',
name: 'Fido'
},
{
type: 'fish',
name: 'Nemo'
}
];
In order for a pet to be considered valid, it must match one of these 3 exact shapes (i.e. both the key and the value must match). For example, this is a valid pet:
{
type: 'dog',
name: 'Fido'
}
However, these are not valid:
{
age: 'dog',
name: 'Fido'
},
and
{
type: 'dog',
name: 'Sammy'
},
and so on.
Since my actual object is much larger than this, I don't want to manually define this type. Is there a way to do it dynamically?
I attempted the following, but it did not work:
type Pet = {
[key in keyof typeof pets]: typeof pets[key];
};