You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeDataType={value: (Data&Extra)|Data;};typeData={type1: string;};typeExtra={isEditable: boolean;onChange: ()=>void;};constdata: 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)
The text was updated successfully, but these errors were encountered:
The value you've provided is not a valid Data & Extra, but it is a valid Data (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.
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.
Bug Report
β― Playground Link
Playground link with relevant code
π» Code
π 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)The text was updated successfully, but these errors were encountered: