typescript - 如何在泛型函数中引用“typeof T”?
问题描述
function isSameOrSubclass<T>(target:any,reference:typeof T):target is T
{
const targetIsReference = target===reference,
targetIsTruthy = target&&true,
targetHasPrototype = "prototype" in target,
targetIsSubclass = targetIsTruthy&&targetHasPrototype&&(target.prototype instanceof reference)
return targetIsReference||targetIsSubclass;
}
编译器失败并出现以下错误:
TS2693:'T' 仅指一种类型,但在此处用作值。
替换typeof T
为new()=>T
允许编译,但不允许这样做:
isSameOrSubclass<Class>(foo,Class)
因为
'typeof Class' 类型的参数不能分配给'new () => Class' 类型的参数。
解决方案
T
需要限制为extends new() => {}
*或类似的,然后直接使用(而不是 via typeof
),如下所示:
function isSameOrSubclass<T extends new() => {}>(target:any,reference: T):target is T
// −−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^^^^^^^^−−−−−−−−−−−−−−−−−−−−−−−−^
{
const targetIsReference = target===reference,
targetIsTruthy = target&&true,
targetHasPrototype = "prototype" in target,
targetIsSubclass = targetIsTruthy&&targetHasPrototype&&(target.prototype instanceof reference)
return targetIsReference||targetIsSubclass;
}
*或类似:该类型new() => {}
指的是具有零参数构造函数的类。因此,要接受具有一个或多个参数的构造函数的类,需要不同的类型。IEnew(...args) => {}
然后你在没有任何显式类型参数的情况下使用它,如下所示:
class Parent {}
class Child extends Parent {}
class Unrelated {}
console.log(isSameOrSubclass(Child, Parent)); // true
console.log(isSameOrSubclass(Parent, Parent)); // true
console.log(isSameOrSubclass(Parent, Unrelated)); // false
推荐阅读
- java - 如何将所有文本字段放入数组中?
- excel - Microsoft Word 和 Excel 邮件合并偶尔会切断最终合并内容的第一个字母
- c - 类型特定的功能
- flutter - 如何在 CircleAvatar 周围放置图标
- python - 在多索引数据框中定位特定数据
- react-native - 任务:app:bundleReleaseJsAndAssets 在捆绑包中失败
- python - 可以创建自定义语言环境,例如 fr_EN?
- python-3.x - 缺少属性时具有类属性的 BeautifulSoup find_all
- r - 防止来自 RStudio 的持久数据库连接
- node.js - 在节点js中使用sharp进行图像压缩