Skip to content

Commit 53d3921

Browse files
committed
Forbid 'this' as constructor parameter type
1 parent e321859 commit 53d3921

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4420,12 +4420,19 @@ namespace ts {
44204420
let container = getThisContainer(node, /*includeArrowFunctions*/ false);
44214421
let parent = container && container.parent;
44224422
if (parent && (isClassLike(parent) || parent.kind === SyntaxKind.InterfaceDeclaration)) {
4423-
if (!(container.flags & NodeFlags.Static)) {
4423+
if (!(container.flags & NodeFlags.Static) && !isConstructorParameter(node, container)) {
44244424
return getDeclaredTypeOfClassOrInterface(getSymbolOfNode(parent)).thisType;
44254425
}
44264426
}
44274427
error(node, Diagnostics.A_this_type_is_available_only_in_a_non_static_member_of_a_class_or_interface);
44284428
return unknownType;
4429+
4430+
function isConstructorParameter(node: TypeNode, container: Node) {
4431+
if (container.kind === SyntaxKind.Constructor) {
4432+
let ctor = (<ConstructorDeclaration>container);
4433+
return !ctor.body.statements.some(st => st === node.parent);
4434+
}
4435+
}
44294436
}
44304437

44314438
function getTypeFromThisTypeNode(node: TypeNode): Type {

0 commit comments

Comments
 (0)