Use contextual type to determine 'this' when determining member visibility (#56105)

This commit is contained in:
Ryan Cavanaugh
2024-08-02 15:22:20 -07:00
committed by GitHub
parent 269219f68e
commit aafdfe5b3f
6 changed files with 321 additions and 2 deletions

View File

@@ -33667,10 +33667,22 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
function getEnclosingClassFromThisParameter(node: Node): InterfaceType | undefined {
// 'this' type for a node comes from, in priority order...
// 1. The type of a syntactic 'this' parameter in the enclosing function scope
const thisParameter = getThisParameterFromNodeContext(node);
let thisType = thisParameter?.type && getTypeFromTypeNode(thisParameter.type);
if (thisType && thisType.flags & TypeFlags.TypeParameter) {
thisType = getConstraintOfTypeParameter(thisType as TypeParameter);
if (thisType) {
// 2. The constraint of a type parameter used for an explicit 'this' parameter
if (thisType.flags & TypeFlags.TypeParameter) {
thisType = getConstraintOfTypeParameter(thisType as TypeParameter);
}
}
else {
// 3. The 'this' parameter of a contextual type
const thisContainer = getThisContainer(node, /*includeArrowFunctions*/ false, /*includeClassComputedPropertyName*/ false);
if (isFunctionLike(thisContainer)) {
thisType = getContextualThisParameterType(thisContainer);
}
}
if (thisType && getObjectFlags(thisType) & (ObjectFlags.ClassOrInterface | ObjectFlags.Reference)) {
return getTargetType(thisType) as InterfaceType;