Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid this as constructor parameter type #5474

Merged
merged 6 commits into from
Nov 2, 2015

Conversation

sandersn
Copy link
Member

Fixes #5449. Thanks @ahejlsberg for pointing out how to fix this. If you have a better way to find out whether something is a constructor parameter, please do suggest it.

function isConstructorParameter(node: TypeNode, container: Node) {
if (container.kind === SyntaxKind.Constructor) {
let ctor = (<ConstructorDeclaration>container);
return !ctor.body.statements.some(st => st === node.parent);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't going to work. The right way to do it is to walk up the parent chain from node and see if you hit the body of the constructor before you get to the constructor itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the some method on Array is an ES5 thing. We restrict ourselves to ES3 so we can run anywhere. In this particular case I'd use the forEach helper function. But, as I said above, there's a better way to do the test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know about some. I switched to walking up the parent chain.

@DanielRosenwasser
Copy link
Member

Could we just consider disallowing this to be used as a parameter type? It's potential for users to shoot themselves in the foot anyway:

class A {
    private a: number;

    getA() {
        return this.a;
    }

    foo(other: this) {
        return other.getA() === this.getA();
    }
}

class B extends A {
    private b: string;

    getB() {
        return this.b;
    }

    foo(other: this) {
        return super.foo(other) && other.getB() === this.getB();
    }
}

let b = new B();

let a1 = new A();
let a2: A = b;

// Valid & runs fine
a1.foo(a2)
a1.foo(b);

// Valid & crashes
a2.foo(a1);

@@ -4420,12 +4420,26 @@ 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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the isConstructorParameter function I would just modify the existing test:

if (!(container.flags & NodeFlags.Static) &&
    (container.kind !== SyntaxKind.Constructor || isNodeDescendentOf(node, (<ConstructorDeclaration>container).body))) {
    ...
}

The isNodeDescendentOf is currently a local helper in emitter.ts, so you need to move it to utilities.ts so it can be shared.

}
class Derived {
n: number;
constructor(public host: Generic<this>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use this in a type annotation of a local variable to test that this can be referenced in the body of a constructor.

@ahejlsberg
Copy link
Member

👍 Once you address my comment about the test.

The test already had a reference to the `this` value, but that doesn't
show that the *type* is allowed.
sandersn added a commit that referenced this pull request Nov 2, 2015
…rameter-type

Forbid this as constructor parameter type
@sandersn sandersn merged commit cc64210 into master Nov 2, 2015
@sandersn sandersn deleted the forbid-this-as-constructor-parameter-type branch November 2, 2015 19:04
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Argument of type 'this' is not assignable to parameter of type 'this'.
5 participants