mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
Use contextual type to determine 'this' when determining member visibility (#56105)
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user