Skip to content

Commit

Permalink
Forbid 'this' as constructor parameter type
Browse files Browse the repository at this point in the history
  • Loading branch information
sandersn committed Nov 2, 2015
1 parent e321859 commit 53d3921
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4420,12 +4420,19 @@ 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)) {
if (!(container.flags & NodeFlags.Static) && !isConstructorParameter(node, container)) {
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: TypeNode, container: Node) {
if (container.kind === SyntaxKind.Constructor) {
let ctor = (<ConstructorDeclaration>container);
return !ctor.body.statements.some(st => st === node.parent);
}
}
}

function getTypeFromThisTypeNode(node: TypeNode): Type {
Expand Down

0 comments on commit 53d3921

Please sign in to comment.