Closed
Description
Bug Report
β― Playground Link
Playground link with relevant code
π» Code
type DataType = {
value: (Data & Extra) | Data;
};
type Data = {
type1: string;
};
type Extra = {
isEditable: boolean;
onChange: () => void;
};
const data: DataType = {
// value should throw an error (onChange is not specified)
value: {
type1: 'type',
isEditable: true,
},
};
π Actual behavior
TypeScript ignores this definition ((Data & Extra) | Data
)
π Expected behavior
I expect TypeScript to warn me that a property within value
is missing: e.g (onChange
is not specified)
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
fatcerberus commentedon Sep 19, 2022
The value you've provided is not a valid
Data & Extra
, but it is a validData
(the types aren't exact, objects can have extra properties). Since the type of.value
is(Data & Extra) | Data
, there is no type error in this code.fatcerberus commentedon Sep 19, 2022
Just to reiterate/clarify: Because objects can have more properties than their type calls for, unions of object types are not mutually exclusive. If you want a mutually exclusive type you need to use a discriminated union.
MartinJohns commentedon Sep 19, 2022
Also related: #20863
typescript-bot commentedon Sep 21, 2022
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.