If I have a type definition like this:
A 'Person' must have either the name
or fullname
property defined.
type Person = {
[k in "name" | "fullname"]: string;
};
If I want to add one more required property, such as age
, my initial instinct would be to do this:
type Person = {
[k in "name" | "fullname"]: string;
age: number; // This will cause an error
};
However, this syntax will not work. The only way to achieve this is by using the intersection operator &
like so:
type Person = {
[k in "name" | "fullname"]: string;
} & { age: number };