TypeScript 类型为'this'的参数不能分配给类型为'NonNullable'的参数?

e4yzc0pl  于 8个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(146)

TypeScript版本: 4.0.3
搜索词: 'this' 不能分配给 'NonNullable'
代码

  1. abstract class TreeNode {
  2. parent: this | null = null; // polymorph this
  3. left: this | null = null;
  4. right: this | null = null;
  5. replaceChild(child: this, replacement: this | null = null) { // this - polymorph type
  6. this.left = child;
  7. this.right = replacement;
  8. }
  9. removeNode() {
  10. this.parent?.replaceChild(this, null); // ERROR
  11. }
  12. removeNode2Way() {
  13. if (this.parent) {
  14. this.parent.replaceChild(this, null); // OK
  15. }
  16. }
  17. }

预期行为:

无错误地进行条件调用。

实际行为:

错误:类型为 'this' 的参数不能分配给类型为 'NonNullable' 的参数。

**Playground链接:**Playground
相关问题:

0kjbasz6

0kjbasz61#

看起来,原则上应该缩小 .?. 左侧的任何引用,但我们对控制流图有顾虑。我们可以接受一个实验PR,但我们不承诺这样做,尤其是如果存在性能/内存影响的话。

相关问题