Skip to content

Commit aa26980

Browse files
committed
Addressing CR feedback
1 parent f57991e commit aa26980

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/compiler/checker.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -3352,6 +3352,10 @@ namespace ts {
33523352
return undefined;
33533353
}
33543354

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.
33553359
function isKnownProperty(type: Type, name: string): boolean {
33563360
if (type.flags & TypeFlags.ObjectType) {
33573361
var resolved = resolveStructuredTypeMembers(type);
@@ -4526,13 +4530,17 @@ namespace ts {
45264530
}
45274531
}
45284532

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)) {
45314535
if (reportErrors) {
45324536
reportRelationError(headMessage, source, target);
45334537
}
45344538
return Ternary.False;
45354539
}
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.
45364544
source = getRegularTypeOfObjectLiteral(source);
45374545
}
45384546

@@ -4619,7 +4627,7 @@ namespace ts {
46194627
return Ternary.False;
46204628
}
46214629

4622-
function hasExcessProperties(source: ObjectType, target: Type, reportErrors: boolean): boolean {
4630+
function hasExcessProperties(source: FreshObjectLiteralType, target: Type, reportErrors: boolean): boolean {
46234631
for (let prop of getPropertiesOfObjectType(source)) {
46244632
if (!isKnownProperty(target, prop.name)) {
46254633
if (reportErrors) {

src/compiler/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,9 @@ namespace ts {
18421842
}
18431843

18441844
/* @internal */
1845+
// Object literals are initially marked fresh. Freshness disappears following an assignment,
1846+
// before a type assertion, or when when an object literal's type is widened. The regular
1847+
// version of a fresh type is identical except for the TypeFlags.FreshObjectLiteral flag.
18451848
export interface FreshObjectLiteralType extends ResolvedType {
18461849
regularType: ResolvedType; // Regular version of fresh type
18471850
}

0 commit comments

Comments
 (0)