TypeScript版本: 4.0.3
搜索词: 'this' 不能分配给 'NonNullable'
代码
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
}
}
}
预期行为:
无错误地进行条件调用。
实际行为:
错误:类型为 'this' 的参数不能分配给类型为 'NonNullable' 的参数。
**Playground链接:**Playground
相关问题:
1条答案
按热度按时间0kjbasz61#
看起来,原则上应该缩小
.
或?.
左侧的任何引用,但我们对控制流图有顾虑。我们可以接受一个实验PR,但我们不承诺这样做,尤其是如果存在性能/内存影响的话。