From 853607fcfd09828c73fe14747ed12b99b0055387 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Thu, 13 Oct 2016 13:16:55 -0700 Subject: [PATCH] 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. --- src/compiler/checker.ts | 10 ++++++---- tests/cases/fourslash/memberListOnContextualThis.ts | 12 ++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 tests/cases/fourslash/memberListOnContextualThis.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 30a0bbd2042c8..2aac2d681bc96 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4776,9 +4776,6 @@ namespace ts { if (isJSConstructSignature) { minArgumentCount--; } - if (!thisParameter && isObjectLiteralMethod(declaration)) { - thisParameter = getContextualThisParameter(declaration); - } const classType = declaration.kind === SyntaxKind.Constructor ? getDeclaredTypeOfClassOrInterface(getMergedSymbol((declaration.parent).symbol)) @@ -9429,8 +9426,13 @@ namespace ts { return getInferredClassType(classSymbol); } } + let thisType = getThisTypeOfDeclaration(container); + if (thisType) { + return thisType; + } - const thisType = getThisTypeOfDeclaration(container); + const thisParameter = getContextualThisParameter(container); + thisType = thisParameter && getTypeOfSymbol(thisParameter); if (thisType) { return thisType; } diff --git a/tests/cases/fourslash/memberListOnContextualThis.ts b/tests/cases/fourslash/memberListOnContextualThis.ts new file mode 100644 index 0000000000000..7eeb67dc62046 --- /dev/null +++ b/tests/cases/fourslash/memberListOnContextualThis.ts @@ -0,0 +1,12 @@ +/// +////interface A { +//// a: string; +////} +////declare function ctx(callback: (this: A) => string): string; +////ctx(function () { return th/*1*/is./*2*/a }); + +goTo.marker('1'); +verify.quickInfoIs("this: A"); +goTo.marker('2'); +verify.memberListContains('a', '(property) A.a: string'); +