Skip to content

Commit 1aa2562

Browse files
committed
Give correct info for contextually typed this
This essentially undoes #9746, which only contextually typed object literal methods, but did so early, during `getSignatureFromDeclaration`. The compiler now contextually types everything, as it did before, but only looks for a contextual type if there is no declared `this` type. After #9746, a member list request from the language service would call `getSignatureFromDeclaration`, but it would never get a contextual this-type, because contextual typing was only enabled for object literal methods.
1 parent 7eb39cc commit 1aa2562

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4776,9 +4776,6 @@ namespace ts {
47764776
if (isJSConstructSignature) {
47774777
minArgumentCount--;
47784778
}
4779-
if (!thisParameter && isObjectLiteralMethod(declaration)) {
4780-
thisParameter = getContextualThisParameter(declaration);
4781-
}
47824779

47834780
const classType = declaration.kind === SyntaxKind.Constructor ?
47844781
getDeclaredTypeOfClassOrInterface(getMergedSymbol((<ClassDeclaration>declaration.parent).symbol))
@@ -9429,8 +9426,13 @@ namespace ts {
94299426
return getInferredClassType(classSymbol);
94309427
}
94319428
}
9429+
let thisType = getThisTypeOfDeclaration(container);
9430+
if (thisType) {
9431+
return thisType;
9432+
}
94329433

9433-
const thisType = getThisTypeOfDeclaration(container);
9434+
const thisParameter = getContextualThisParameter(container);
9435+
thisType = thisParameter && getTypeOfSymbol(thisParameter);
94349436
if (thisType) {
94359437
return thisType;
94369438
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts'/>
2+
////interface A {
3+
//// a: string;
4+
////}
5+
////declare function ctx(callback: (this: A) => string): string;
6+
////ctx(function () { return th/*1*/is./*2*/a });
7+
8+
goTo.marker('1');
9+
verify.quickInfoIs("this: A");
10+
goTo.marker('2');
11+
verify.memberListContains('a', '(property) A.a: string');
12+

0 commit comments

Comments
 (0)