Repository navigation
Argument of type 'this' is not assignable to parameter of type 'this'. #5449
Description
Activity
- I'm not getting any errors on the latest bits from
master - this is incorrect ES6 code since
thisis used beforesupercall
- I'm not getting any errors on the latest bits from
Vladimir Matveev (@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); } }
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
The bug here is that the
thistype 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 thethistype is not in scope in static members.Nathan Shively-Sanders (@sandersn) can you add the error check for release-1.7
sandersn commented
on Oct 30, 2015 MemberMore actionsThere is an existing error for this, it's just getting missed now. I'll figure out why.
Nathan Shively-Sanders (@sandersn) The issue is in the
getThisTypemethod. 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).- added a commit that references this issue
on Oct 30, 2015 So I just made my use case dead? I don't understand why we need to disallow
thistype in constructor parameter list.vilic The use case was dead all along, we just weren't reporting an error properly. The compiler isn't equipped to handle
thistypes 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?Anders Hejlsberg (@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.
- added a commit that references this issue
on Nov 2, 2015 - locked and limited conversation to collaborators
on Jun 19, 2018
This error also appears on generics with
thisto thesupercall (which is the real use case).