Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
checnev opened this issue Oct 30, 2020 · 1 comment
Labels
Effort: Difficult Good luck. Experimentation Needed Someone needs to try this out to see what happens Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@checnev
Copy link

checnev commented Oct 30, 2020

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:

@DanielRosenwasser
Copy link
Member

Seems like any reference to the left of a . or ?. should in principle be narrowed, but we have concerns around the control flow graph. We would accept an experiment PR, but we are not committed to this, especially if there are perf/memory ramifications.

@DanielRosenwasser DanielRosenwasser added Experiment A fork with an experimental idea which might not make it into master Help Wanted You can do this Suggestion An idea for TypeScript labels Nov 2, 2020
@DanielRosenwasser DanielRosenwasser added this to the Backlog milestone Nov 2, 2020
@DanielRosenwasser DanielRosenwasser added Effort: Difficult Good luck. Experimentation Needed Someone needs to try this out to see what happens and removed Experiment A fork with an experimental idea which might not make it into master labels Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Effort: Difficult Good luck. Experimentation Needed Someone needs to try this out to see what happens Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants