Microsoft/TypeScript

Argument of type 'this' is not assignable to parameter of type 'NonNullable'?

Open

#41.340 aperta il 30 ott 2020

Vedi su GitHub
 (1 commento) (0 reazioni) (0 assegnatari)TypeScript (6726 fork)batch import
Effort: DifficultExperimentation NeededHelp WantedSuggestion

Metriche repository

Star
 (48.455 star)
Metriche merge PR
 (Merge medio 6g 17h) (9 PR mergiate in 30 g)

Descrizione

TypeScript Version: 4.0.3

Search Terms: 'this' is not assignable to 'NonNullable'

Code

abstract class TreeNode {
  parent: this | null = null; // polymorph this
  left: this | null = null;
  right: this | null = null;

  replaceChild(child: this, replacement: this | null = null) { // this - polymorph type
    this.left = child;
    this.right = replacement;
  }

  removeNode() {
    this.parent?.replaceChild(this, null); // ERROR
  }

  removeNode2Way() {
    if (this.parent) {
      this.parent.replaceChild(this, null); // OK
    }
  }
}

Expected behavior: Conditional call without errors.

Actual behavior: Error: Argument of type 'this' is not assignable to parameter of type 'NonNullable'

Playground Link: Playground

Related Issues:

Guida contributor