有人可以解释为什么 Typescript 中不允许使用以下内容吗?
interface Base<T> {
}
interface SomeInterface<First, Second extends Base<First>> {
}
由于某种原因我不明白,无法引用类型 First
在Base<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/