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

Argument of type 'this' is not assignable to parameter of type 'this'. #5449

Closed
vilicvane opened this issue Oct 29, 2015 · 10 comments · Fixed by #5474
Closed

Argument of type 'this' is not assignable to parameter of type 'this'. #5449

vilicvane opened this issue Oct 29, 2015 · 10 comments · Fixed by #5474
Assignees
Labels
Bug A bug in TypeScript

Comments

@vilicvane
Copy link
Contributor

This error also appears on generics with this to the super call (which is the real use case).

class A {
    constructor(bar: this) { }
    foo(bar: this) { }
}

class B extends A {
    constructor() {
        super(this); // Error
        this.foo(this); // Works
    }
}
@vladima
Copy link
Contributor

vladima commented Oct 29, 2015

  1. I'm not getting any errors on the latest bits from master
  2. this is incorrect ES6 code since this is used before super call

@vilicvane
Copy link
Contributor Author

@vladima Don't know why no.2 is causing the same error but I did more test with the latest "next" release and found the actual issue:

class Test<TSample> {
    // this is the property triggering the issue.
    sample: TSample;

    constructor() { }
}

class Sample {
    constructor(
        public host: Test<this>
    ) { }
}

class TerminalSample extends Sample {
    constructor(host: Test<this>) {
        super(host);
    }
}

@mhegazy
Copy link
Contributor

mhegazy commented Oct 29, 2015

here is a simpler example:

class Base {
    constructor(a: this) { }
}

class Derived extends Base {
    prop: string;
}

var base: Base;
var derived: Derived;

new Base(base); // Error should be OK
new Derived(derived); // Error should be OK 
new Derived(base); // Error

@mhegazy mhegazy added the Bug A bug in TypeScript label Oct 29, 2015
@ahejlsberg
Copy link
Member

The bug here is that the this type should not be in scope in the parameter list of a constructor (but it should be in scope within the constructor body). This is similar to how the this type is not in scope in static members.

@mhegazy mhegazy assigned sandersn and unassigned ahejlsberg Oct 30, 2015
@mhegazy mhegazy added this to the TypeScript 1.7 milestone Oct 30, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Oct 30, 2015

@sandersn can you add the error check for release-1.7

@sandersn
Copy link
Member

There is an existing error for this, it's just getting missed now. I'll figure out why.

@ahejlsberg
Copy link
Member

@sandersn The issue is in the getThisType method. When the container is a constructor we should be checking that the node is in the body of the constructor (and thus not in the parameter list).

@vilicvane
Copy link
Contributor Author

So I just made my use case dead? I don't understand why we need to disallow this type in constructor parameter list.

@ahejlsberg
Copy link
Member

@vilic The use case was dead all along, we just weren't reporting an error properly. The compiler isn't equipped to handle this types in the static side of a class (which includes the constructor) and it would take a substantial amount of work to implement it. Is there a particular scenario where this would be useful?

@vilicvane
Copy link
Contributor Author

@ahejlsberg It's actually quite like what I wrote, more specifically:

abstract class Test<TSample> {
    sample: TSample;
}

class SomeTest extends Test<SomeSample> {

}

abstract class Sample {
    constructor(
        public host: Test<this>
    ) { }    
}

class SomeSample extends Sample {
    constructor(host: Test<this>) {
        super(host);
        host.sample = this;
    }
}

let someTest = new SomeTest<SomeSample>();
let someSample = new SomeSample(someTest);
someTest.host.sample; // expecting type `SomeSample`.

Actually I ended up with another implementation (as this is messing things up), but I think the scenario should be reasonable. I didn't realize that constructors have different contexts than other instance methods, but certainly it could wait long enough if it's not something trivial to do.

sandersn added a commit that referenced this issue Nov 2, 2015
@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
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants