Skip to content

Commit 529fcfd

Browse files
committed
Assignment compatibility fix / contextual intersection types
1 parent d571034 commit 529fcfd

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,8 +3160,8 @@ namespace ts {
31603160
return emptyArray;
31613161
}
31623162

3163-
// If the given type is an object type and that type has a property by the given name, return
3164-
// the symbol for that property. Otherwise return undefined.
3163+
// If the given type is an object type and that type has a property by the given name,
3164+
// return the symbol for that property.Otherwise return undefined.
31653165
function getPropertyOfObjectType(type: Type, name: string): Symbol {
31663166
if (type.flags & TypeFlags.ObjectType) {
31673167
let resolved = resolveStructuredTypeMembers(<ObjectType>type);
@@ -4464,7 +4464,7 @@ namespace ts {
44644464
let reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo;
44654465
// identity relation does not use apparent type
44664466
let sourceOrApparentType = relation === identityRelation ? source : getApparentType(source);
4467-
if (sourceOrApparentType.flags & TypeFlags.ObjectType && target.flags & TypeFlags.ObjectType) {
4467+
if (sourceOrApparentType.flags & (TypeFlags.ObjectType | TypeFlags.Intersection) && target.flags & TypeFlags.ObjectType) {
44684468
if (result = objectTypeRelatedTo(sourceOrApparentType, <ObjectType>target, reportStructuralErrors)) {
44694469
errorInfo = saveErrorInfo;
44704470
return result;
@@ -4595,7 +4595,7 @@ namespace ts {
45954595
// Third, check if both types are part of deeply nested chains of generic type instantiations and if so assume the types are
45964596
// equal and infinitely expanding. Fourth, if we have reached a depth of 100 nested comparisons, assume we have runaway recursion
45974597
// and issue an error. Otherwise, actually compare the structure of the two types.
4598-
function objectTypeRelatedTo(source: ObjectType, target: ObjectType, reportErrors: boolean): Ternary {
4598+
function objectTypeRelatedTo(source: Type, target: Type, reportErrors: boolean): Ternary {
45994599
if (overflow) {
46004600
return Ternary.False;
46014601
}
@@ -4670,7 +4670,7 @@ namespace ts {
46704670
return result;
46714671
}
46724672

4673-
function propertiesRelatedTo(source: ObjectType, target: ObjectType, reportErrors: boolean): Ternary {
4673+
function propertiesRelatedTo(source: Type, target: Type, reportErrors: boolean): Ternary {
46744674
if (relation === identityRelation) {
46754675
return propertiesIdenticalTo(source, target);
46764676
}
@@ -4753,7 +4753,10 @@ namespace ts {
47534753
return result;
47544754
}
47554755

4756-
function propertiesIdenticalTo(source: ObjectType, target: ObjectType): Ternary {
4756+
function propertiesIdenticalTo(source: Type, target: Type): Ternary {
4757+
if (!(source.flags & TypeFlags.ObjectType && target.flags & TypeFlags.ObjectType)) {
4758+
return Ternary.False;
4759+
}
47574760
let sourceProperties = getPropertiesOfObjectType(source);
47584761
let targetProperties = getPropertiesOfObjectType(target);
47594762
if (sourceProperties.length !== targetProperties.length) {
@@ -4774,7 +4777,7 @@ namespace ts {
47744777
return result;
47754778
}
47764779

4777-
function signaturesRelatedTo(source: ObjectType, target: ObjectType, kind: SignatureKind, reportErrors: boolean): Ternary {
4780+
function signaturesRelatedTo(source: Type, target: Type, kind: SignatureKind, reportErrors: boolean): Ternary {
47784781
if (relation === identityRelation) {
47794782
return signaturesIdenticalTo(source, target, kind);
47804783
}
@@ -4900,7 +4903,7 @@ namespace ts {
49004903
return result & isRelatedTo(s, t, reportErrors);
49014904
}
49024905

4903-
function signaturesIdenticalTo(source: ObjectType, target: ObjectType, kind: SignatureKind): Ternary {
4906+
function signaturesIdenticalTo(source: Type, target: Type, kind: SignatureKind): Ternary {
49044907
let sourceSignatures = getSignaturesOfType(source, kind);
49054908
let targetSignatures = getSignaturesOfType(target, kind);
49064909
if (sourceSignatures.length !== targetSignatures.length) {
@@ -4917,7 +4920,7 @@ namespace ts {
49174920
return result;
49184921
}
49194922

4920-
function stringIndexTypesRelatedTo(source: ObjectType, target: ObjectType, reportErrors: boolean): Ternary {
4923+
function stringIndexTypesRelatedTo(source: Type, target: Type, reportErrors: boolean): Ternary {
49214924
if (relation === identityRelation) {
49224925
return indexTypesIdenticalTo(IndexKind.String, source, target);
49234926
}
@@ -4942,7 +4945,7 @@ namespace ts {
49424945
return Ternary.True;
49434946
}
49444947

4945-
function numberIndexTypesRelatedTo(source: ObjectType, target: ObjectType, reportErrors: boolean): Ternary {
4948+
function numberIndexTypesRelatedTo(source: Type, target: Type, reportErrors: boolean): Ternary {
49464949
if (relation === identityRelation) {
49474950
return indexTypesIdenticalTo(IndexKind.Number, source, target);
49484951
}
@@ -4975,7 +4978,7 @@ namespace ts {
49754978
return Ternary.True;
49764979
}
49774980

4978-
function indexTypesIdenticalTo(indexKind: IndexKind, source: ObjectType, target: ObjectType): Ternary {
4981+
function indexTypesIdenticalTo(indexKind: IndexKind, source: Type, target: Type): Ternary {
49794982
let targetType = getIndexTypeOfType(target, indexKind);
49804983
let sourceType = getIndexTypeOfType(source, indexKind);
49814984
if (!sourceType && !targetType) {
@@ -6335,7 +6338,7 @@ namespace ts {
63356338

63366339
function getTypeOfPropertyOfContextualType(type: Type, name: string) {
63376340
return applyToContextualType(type, t => {
6338-
let prop = getPropertyOfObjectType(t, name);
6341+
let prop = t.flags & TypeFlags.StructuredType ? getPropertyOfType(t, name) : undefined;
63396342
return prop ? getTypeOfSymbol(prop) : undefined;
63406343
});
63416344
}

0 commit comments

Comments
 (0)