diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6ea8d896326..80bc58217ae 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4420,26 +4420,13 @@ namespace ts { let container = getThisContainer(node, /*includeArrowFunctions*/ false); let parent = container && container.parent; if (parent && (isClassLike(parent) || parent.kind === SyntaxKind.InterfaceDeclaration)) { - if (!(container.flags & NodeFlags.Static) && !isConstructorParameter(node, container)) { + if (!(container.flags & NodeFlags.Static) && + (container.kind !== SyntaxKind.Constructor || isNodeDescendentOf(node, (container).body))) { return getDeclaredTypeOfClassOrInterface(getSymbolOfNode(parent)).thisType; } } error(node, Diagnostics.A_this_type_is_available_only_in_a_non_static_member_of_a_class_or_interface); return unknownType; - - function isConstructorParameter(node: Node, container: Node) { - if (container.kind === SyntaxKind.Constructor) { - let ctor = (container); - while (node && node !== ctor) { - if (node === ctor.body) { - return false; - } - node = node.parent; - } - - return true; - } - } } function getTypeFromThisTypeNode(node: TypeNode): Type { diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index b2fa553a737..abcea979dd9 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -359,14 +359,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi sourceMaps: sourceMapDataList }; - function isNodeDescendentOf(node: Node, ancestor: Node): boolean { - while (node) { - if (node === ancestor) return true; - node = node.parent; - } - return false; - } - function isUniqueLocalName(name: string, container: Node): boolean { for (let node = container; isNodeDescendentOf(node, container); node = node.nextContainer) { if (node.locals && hasProperty(node.locals, name)) { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index fd991039d3e..9b8948d6cc4 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1167,6 +1167,14 @@ namespace ts { return !!node && (node.kind === SyntaxKind.ArrayBindingPattern || node.kind === SyntaxKind.ObjectBindingPattern); } + export function isNodeDescendentOf(node: Node, ancestor: Node): boolean { + while (node) { + if (node === ancestor) return true; + node = node.parent; + } + return false; + } + export function isInAmbientContext(node: Node): boolean { while (node) { if (node.flags & (NodeFlags.Ambient | NodeFlags.DeclarationFile)) {