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
typeMyPhotoA={id: number};typeMyPhotoB=MyPhotoA&{userId: number};typeMyPhoto=MyPhotoA|MyPhotoB;declareconstmyPhoto: MyPhoto;constmyPhoto2: MyPhoto={
...myPhoto,// Expected type error but got none. `userId` should be stringuserId: {},};if('userId'inmyPhoto2){// No type error, but runtime exception!! Very bad…myPhoto2.userId.toFixed();}
IIUC, this is because TS considers myPhoto2 assignable to MyPhotoA with an excess property userId.
Shouldn't TS check that the excess property is not used in other members of the union?
The text was updated successfully, but these errors were encountered:
OliverJAsh
changed the title
Property not checked for union type
Property not checked in union of objects
Mar 28, 2019
The real design issue here is that excess property checking runs exclusively before further type-checking, delivering a yes/no answer. To correctly check this case you need excess property checking to effectively prune off MyPhotoA from the union MyPhoto when comparing the types of the properties. For that to work you either need excess property checking to work in tandem with type-checking, or you need excess property checking to return more than just yes/no.
TypeScript Version: 3.3.3333
Search Terms:
Code
IIUC, this is because TS considers
myPhoto2
assignable toMyPhotoA
with an excess propertyuserId
.Shouldn't TS check that the excess property is not used in other members of the union?
The text was updated successfully, but these errors were encountered: