@@ -3352,6 +3352,10 @@ namespace ts {
3352
3352
return undefined;
3353
3353
}
3354
3354
3355
+ // Check if a property with the given name is known anywhere in the given type. In an object
3356
+ // type, a property is considered known if the object type is empty, if it has any index
3357
+ // signatures, or if the property is actually declared in the type. In a union or intersection
3358
+ // type, a property is considered known if it is known in any constituent type.
3355
3359
function isKnownProperty(type: Type, name: string): boolean {
3356
3360
if (type.flags & TypeFlags.ObjectType) {
3357
3361
var resolved = resolveStructuredTypeMembers(type);
@@ -4526,13 +4530,17 @@ namespace ts {
4526
4530
}
4527
4531
}
4528
4532
4529
- if (relation === assignableRelation && source.flags & TypeFlags.ObjectLiteral && source.flags & TypeFlags.FreshObjectLiteral) {
4530
- if (hasExcessProperties(<ObjectType >source, target, reportErrors)) {
4533
+ if (relation !== identityRelation && source.flags & TypeFlags.FreshObjectLiteral) {
4534
+ if (hasExcessProperties(<FreshObjectLiteralType >source, target, reportErrors)) {
4531
4535
if (reportErrors) {
4532
4536
reportRelationError(headMessage, source, target);
4533
4537
}
4534
4538
return Ternary.False;
4535
4539
}
4540
+ // Above we check for excess properties with respect to the entire target type. When union
4541
+ // and intersection types are further deconstructed on the target side, we don't want to
4542
+ // make the check again (as it might fail for a partial target type). Therefore we obtain
4543
+ // the regular source type and proceed with that.
4536
4544
source = getRegularTypeOfObjectLiteral(source);
4537
4545
}
4538
4546
@@ -4619,7 +4627,7 @@ namespace ts {
4619
4627
return Ternary.False;
4620
4628
}
4621
4629
4622
- function hasExcessProperties(source: ObjectType , target: Type, reportErrors: boolean): boolean {
4630
+ function hasExcessProperties(source: FreshObjectLiteralType , target: Type, reportErrors: boolean): boolean {
4623
4631
for (let prop of getPropertiesOfObjectType(source)) {
4624
4632
if (!isKnownProperty(target, prop.name)) {
4625
4633
if (reportErrors) {
0 commit comments