typescript - Typescript Exclude 拒绝泛型类型
问题描述
这是一个最小的可重现示例
type Value = {}
type Something = {}
function isValid<T>(a: Exclude<T, Value>) { }
function selecter<T extends Something>(obj: T) {
isValid(obj)
}
export { }
Typescript 3.8.3 拒绝isEqual(obj)表达式
Type 'Something' is not assignable to type 'Exclude<T, Value>
但是Type Something是可分配的!这有效:
let s: Something = {}
isValid(s)
错误消息不是错误吗?如何使isValid接受除Value之外的任何类型?
解决方案
为 isValid 的参数键入不匹配。
type Something {}
function isValid<T>(a: T) { }
function selecter<T extends Something>(obj: T) {
isValid(obj)
}
推荐阅读
- qt - 使用静态库构建独立的 qt 应用程序 - 缺少配置命令
- microsoft-edge - 从 Windows 10 build 1809 上的快捷方式以私密模式启动 microsoft edge
- oracle - 错误:将数组传递给另一个过程时出现“错误的数字或类型或参数”
- vxworks - 如何在 VxWorks 7.0 中将 PCI / PCIe 虚拟映射到物理内存?
- c# - 如何覆盖方法保存模板方法模式
- mysql - mysql docker容器的性能问题
- node.js - 如何编辑节点模块
- azure - Azure 函数主机阈值超过线程数
- azure-devops - 如何在微服务中最好地使用 Azure Devops 发布管道?
- ios - 如何使用 RxDataSources 避免重复点击 Tableview 上的按钮