镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@vilicvane

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
    }
}

Activity

  1. vladima commented on Oct 29, 2015

    @vladima
    Contributor
    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
  2. vilicvane commented on Oct 29, 2015

    @vilicvane
    Author

    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);
        }
    }
  3. mhegazy commented on Oct 29, 2015

    @mhegazy
    Contributor

    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
  4. ahejlsberg commented on Oct 30, 2015

    @ahejlsberg
    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.

  5. mhegazy commented on Oct 30, 2015

    @mhegazy
    Contributor

    Nathan Shively-Sanders (@sandersn) can you add the error check for release-1.7

  6. sandersn commented on Oct 30, 2015

    @sandersn
    Member

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

  7. ahejlsberg commented on Oct 30, 2015

    @ahejlsberg
    Member

    Nathan Shively-Sanders (@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).

  8. vilicvane commented on Oct 31, 2015

    @vilicvane
    Author

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

  9. ahejlsberg commented on Oct 31, 2015

    @ahejlsberg
    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?

  10. vilicvane commented on Oct 31, 2015

    @vilicvane
    Author

    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.

  11. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions