Microsoft/TypeScript

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

オープン

#41,340 opened on 2020/10/30

 (1 件のコメント) (0 件のリアクション) (0 人の担当者)TypeScript (13,395 件のフォーク)batch import
Effort: DifficultExperimentation NeededHelp WantedSuggestion

Repository metrics

Stars
 (108,860 個のスター)
PR merge metrics
 (PR metrics pending)

説明

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:

コントリビューターガイド