javascript - typescript - 泛型 - 对同一参数列表中的参数的引用

标签 javascript generics typescript

有人可以解释为什么 Typescript 中不允许使用以下内容吗?

interface Base<T> {
}

interface SomeInterface<First, Second extends Base<First>> {
}

由于某种原因我不明白,无法引用类型 FirstBase<First> .

错误是:类型参数的约束无法引用同一参数列表中的任何类型参数。

最佳答案

根据wiki在 0.9.7 的重大更改中,引入这一点是为了减少开销:

Instead, use constraint arguments that are not in the same argument list, make constraints non-generic, or simply pass 'any' as the parameter to the constraint.

Reason: The added overhead in terms of type-checking, error-reporting, and design complexity did not add enough additional expressiveness to make it worthwhile for 1.0. We may revisit this in future versions of TypeScript.

您的用例正是文档中提到的两个用例之一。

关于javascript - typescript - 泛型 - 对同一参数列表中的参数的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29199802/

相关文章:

javascript - 函数参数内的自动数组解构?

java - 如何从泛型类调用静态方法?

css - Angular:如何使用查询参数过滤数据表

javascript - 如何为类型类编写单元测试?

javascript - Angular 6 event.stopPropagation() 在指令中不起作用(模板驱动形式已经采用 event.data)

javascript - JavaScript 中鼠标悬停在图像缩略图上时图像 slider (循环),鼠标移开时停止

javascript - 使用 d3 将 Bootstrap 工具提示绑定(bind)到动态创建的 SVG 元素

javascript - 用 qwest react 无限滚动

java - 这是 Java 泛型的限制吗?

swift - 在 Swift 协议(protocol)上引用静态变量的正确方法是什么?