I'm trying to define a type which should be either Base
or Base
with additional fields. My code looks like this:
interface Base {
id: string,
}
interface Extra {
firstName: string,
lastName: string,
}
type Human = Base | (Base & Extra);
const validBase: Base = {id: "123"};
const validHuman: Human = {id: "123", firstName: "Seppo", lastName: "Taalasmaa"};
This works as expected. But if I create an invalid Human
, which has only firstName
but no lastName
, it is still considered to be a valid Human
. Why?
const shouldBeInvalid: Human = {id: "123", firstName: "Seppo"}; // Works, why??