typescript - 交集类型如何与多态 this 和泛型结合使用?
问题描述
以下代码片段无法编译,我不明白为什么。抱歉,有点冗长,这是我能找到的最小的 MWE。
interface Num {
num: number;
}
class Base {
num?: number;
base(): this {
return this;
}
addNum(): this & Num {
this.num = 1;
return this as this & Num;
}
}
function unwrapNum<B extends Base>(base: B): B & Num {
return base.addNum().base();
}
它失败并出现错误:
Type 'B' is not assignable to type 'B & Num'.
Type 'Base' is not assignable to type 'B & Num'.
Type 'Base' is not assignable to type 'B'.
'Base' is assignable to the constraint of type 'B', but 'B' could be instantiated with a different subtype of constraint 'Base'.
Type 'B' is not assignable to type 'Num'.
Type 'Base' is not assignable to type 'Num'.
Types of property 'num' are incompatible.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
我的理解是输入类型B
在调用addNum
它之后应该变成B & Num
如果我打破那个分配它看起来的样子。但是,当我调用base()
它时,结果没有B & Num
我期望/想要的类型,而是只有B
,这意味着某些东西没有按预期工作。我的预感是这与多态 this 有关,例如(B & Num).base()
没有 type B & Num
。
但是,如果我使用以下内容删除泛型:
const base = new Base();
const numed: Base & Num = base.addNum();
const numed2: Base & Num = numed.base();
它工作得很好,所以感觉它是通用 Base、多态 this 和交集类型的组合。
奇怪的是,如果包装在另一个泛型中,它确实可以编译:
function unwrapNum<B extends Base>(base: B): B & Num {
function helper<BN extends B & Num>(b: BN): BN {
return b.base();
}
return helper(base.addNum());
}
感谢 Aleksey L. 提供更小的 MWQ
解决方案
推荐阅读
- javascript - 没有在 Django Python 中加载静态文件
- scilab - 如何在 Scilab 应用程序中更改参数值后刷新图形?
- javascript - 模拟 isTrusted=true 到事件监听器 (mouseEvent)
- android - 我的手机未收到消息的 Firebase reg id,仅在模拟器上
- c++ - 在可变参数模板上下文中缺少有关格式说明符的警告
- powershell - Wix 安装程序自定义操作 powershell 脚本未正确执行
- git - 源树无法推送
- .net - 在安装程序项目中使用自定义操作提交 .exe 失败
- python-3.x - 如何在 Odoo 11 中覆盖自定义模型的更新功能
- php - php rest API 发布方法