Skip to content

Commit

Permalink
Cherry-pick PR #50279 into release-4.8 (#50280)
Browse files Browse the repository at this point in the history
Component commits:
eff4406 Revert "Fixed an issue with contextual type for intersection properties (#48668)"
This reverts commit 9236e39.

Co-authored-by: Ryan Cavanaugh <RyanCavanaugh@users.noreply.github.com>
  • Loading branch information
TypeScript Bot and RyanCavanaugh authored Aug 11, 2022
1 parent 60b5167 commit 745f958
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 256 deletions.
34 changes: 4 additions & 30 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27124,37 +27124,16 @@ namespace ts {
}

function getTypeOfPropertyOfContextualType(type: Type, name: __String, nameType?: Type) {
return mapType(type, (t): Type | undefined => {
if (t.flags & TypeFlags.Intersection) {
const intersection = t as IntersectionType;
let newTypes = mapDefined(intersection.types, getTypeOfConcretePropertyOfContextualType);
if (newTypes.length > 0) {
return getIntersectionType(newTypes);
}
newTypes = mapDefined(intersection.types, getTypeOfApplicableIndexInfoOfContextualType);
if (newTypes.length > 0) {
return getIntersectionType(newTypes);
}
return undefined;
}
const concretePropertyType = getTypeOfConcretePropertyOfContextualType(t);
if (concretePropertyType) {
return concretePropertyType;
}
return getTypeOfApplicableIndexInfoOfContextualType(t);
}, /*noReductions*/ true);

function getTypeOfConcretePropertyOfContextualType(t: Type) {
return mapType(type, t => {
if (isGenericMappedType(t) && !t.declaration.nameType) {
const constraint = getConstraintTypeFromMappedType(t);
const constraintOfConstraint = getBaseConstraintOfType(constraint) || constraint;
const propertyNameType = nameType || getStringLiteralType(unescapeLeadingUnderscores(name));
if (isTypeAssignableTo(propertyNameType, constraintOfConstraint)) {
return substituteIndexedMappedType(t, propertyNameType);
}
return undefined;
}
if (t.flags & TypeFlags.StructuredType) {
else if (t.flags & TypeFlags.StructuredType) {
const prop = getPropertyOfType(t, name);
if (prop) {
return isCircularMappedProperty(prop) ? undefined : getTypeOfSymbol(prop);
Expand All @@ -27165,15 +27144,10 @@ namespace ts {
return restType;
}
}
return findApplicableIndexInfo(getIndexInfosOfStructuredType(t), nameType || getStringLiteralType(unescapeLeadingUnderscores(name)))?.type;
}
return undefined;
}
function getTypeOfApplicableIndexInfoOfContextualType(t: Type) {
if (!(t.flags & TypeFlags.StructuredType)) {
return undefined;
}
return findApplicableIndexInfo(getIndexInfosOfStructuredType(t), nameType || getStringLiteralType(unescapeLeadingUnderscores(name)))?.type;
}
}, /*noReductions*/ true);
}

// In an object literal contextually typed by a type T, the contextual type of a property assignment is the type of
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 745f958

Please sign in to comment.