Skip to content

Variance Error when binding class method #3076

@Swatinem

Description

@Swatinem

Consider the following code (try):

class C {
  constructor() {
    this.method = this.method.bind(this);
  }
  //method: () => void; // uncomment this line
  method() {}
}

Which generates the following error:

3:     this.method = this.method.bind(this);
            ^ property `method`. Covariant property `method` incompatible with contravariant use in
3:     this.method = this.method.bind(this);
       ^ assignment of property `method`

As noted in the code, if you add a separate property type declaration, the error is gone. IMO, it does not really make sense to enforce a separate declaration here as the method definition should be enough?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions