Skip to content

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

Open
@checnev

Description

@checnev

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:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions