- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我应该在 typescript 中使用什么类型来表示任何类?
我正在尝试编写一个函数,它接受一个类数组并返回一个具有不同顺序的数组。
function shuffle(classes: typeof Object[]) : typeof Object[] {
return ...;
}
class A { }
class B extends A { }
class C extends B { }
class D extends B { }
shuffle([A, B, C, D]);
Argument of type 'typeof A[]' is not assignable to parameter of type 'ObjectConstructor[]'.
然后我尝试了:
shuffle([typeof A, typeof B, typeof C, typeof D]);
error TS2345: Argument of type 'string[]' is not assignable to parameter of type 'ObjectConstructor[]'.Type 'string' is not assignable to type 'ObjectConstructor'.
什么是正确的方法?泛型?如何?这不起作用:
export function <T extends typeof Object> shuffle(classes: T[]) : T[]
这两者都不是:
export function <T extends Object> sortClassesBySpeciality(classes: typeof T[]) : typeof T[]
还有为什么
好的,明白了,typeof (typeof A)
是
"string"
而
""+ typeof A
是
function
?
typeof
有两种截然不同的含义上下文类型定义和表达式。
(最终目标是根据 Object
的 extends
级别对类进行排序。)
最佳答案
你应该避免在 typescript 中使用 Object
类型,你最好使用 any
作为 the docs say :
You might expect Object to play a similar role, as it does in other languages. But variables of type Object only allow you to assign any value to them - you can’t call arbitrary methods on them, even ones that actually exist
但是如果你想表示类,那么你需要有以下形式:
{ new (): CLASS_TYPE }
或者在你的情况下:
function shuffle(classes: Array<{ new (): any }>): Array<{ new (): any }> {
return [];
}
class A { }
class B extends A { }
class C extends B { }
class D extends B { }
shuffle([A, B, C, D]);
如果您所有的类都基于父类(super class)(正如您的示例所暗示的那样),那么您可以简单地执行以下操作:
function shuffle(classes: Array<{ new (): A }>): Array<{ new (): A }> {
return [];
}
刚刚看到你想要
sort the classes by level of extends from Object
要回答这个问题:
function shuffle(classes: Array<{ new (): any }>): Array<{ new (): any }> {
return classes.sort((a, b) => getInheritanceLevel(a) - getInheritanceLevel(b));
}
function getInheritanceLevel(cls: { new (): any }): number {
let level = 0;
while (Object.getPrototypeOf(cls.prototype) !== Object.prototype) {
level++;
cls = Object.getPrototypeOf(cls.prototype).constructor;
}
return level;
}
shuffle([D, A, C, B]); // returns [A, B, D, C]
关于reflection - Typescript - 代表任何类的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39976329/
我有: func NewMethodDescriptor(typ interface{}) *MethodDescriptor { reflectedMethod := reflect.Val
我需要确定地检查 reflect.Type 是否是一个错误。 错误没有反射(reflect)类型。在 go reflect 中检查类型错误的正式/惯用方式是什么? Go Playground Full
根据 reflect 文档 reflect.Value.MapIndex() 应返回一个 reflect.Value,它表示存储在 map 特定键处的数据的值.所以我的理解是以下两个表达式应该是相同的
与 reflect pkg 有点混淆 所有示例都使用 reflect.NewValue() 来获取 var 的 reflect.Value,但是 func NewValue 未记录在 http://g
在计算机语言的上下文中,我从未找到关于反射的词源的明确解释,所以我想在这里澄清一下。 “Reflection”源于拉丁语,有以下definitions : bend back turn back tu
我写了一个漂亮的函数,它可以接受 system.object ,反射(reflect)其属性并将对象序列化为 JSON 字符串。它看起来像这样: public class JSONSerializer
我正在尝试创建一个函数 import Language.Reflection foo : Type -> TT 我尝试使用reflect 策略: foo = proof { intro t
最近我和一位同事谈论 C++,感叹没有办法获取带有类字段名称的字符串并提取具有该名称的字段;换句话说,它缺乏反射(reflection)。他困惑地看着我,并问什么时候有人需要做这样的事情。 除了“嘿,
我正在考虑允许模块与属性文件中的类一起使用的想法;像 availableModules.properties Contact=org.addressbook.ContactMain Business=
这个问题特别与为具有大量字段的对象覆盖 equals() 方法有关。首先,让我说这个大对象不能在不违反 OO 原则的情况下分解成多个组件,所以告诉我“没有类应该有超过 x 个字段”无济于事。 继续前进
例子 router.Get(path, handler) // works fine methodStr = "Get" router.methodStr(path, handler) // e
我一直坚持使用反射库的问题。由于很多推荐,我决定使用它,但我只是在学习,有些部分并不是很容易.. 我有这部分代码: func countDataByName(sourceName string, s
我有一个包含一些 url 参数的特定结构,我想使用 reflect 构建一个 url 参数字符串以遍历结构字段,这样我就不会关心结构真正包含什么。 假设我有一个这样的结构: type Student
我正在尝试从 reflect.Value 中检索字符串值, 我希望 value.String()成为okok但我得到了相反。 我错过了什么吗? package main import ( "f
为了避免创建 org.reflections.Reflections 类的多个实例,我只想创建一个并根据需要重用。有谁知道这个类是否是线程安全的? 如果它不是线程安全的,我知道我可以使用 Java 的
我最近对引用、具体化和反射(reflection)感到困惑。有人可以很好地解释他们的关系和差异(如果有的话)吗? 最佳答案 引用 这可能是最简单的一个。考虑一下当您在 REPL 中键入以下内容时会发生
less main.go输出: ``` package main import ( "reflect" "net/url" "fmt" ) type User struct {
我在 golang 中使用 gorm 包 ( https://github.com/jinzhu/gorm ) 作为我的数据库库。我有很多类(数据库表),如“酒店”或“套餐”。复制代码不是好的编程习惯
我有代码 var t reflect.Type = LaunchController(route.controller) // create controller ptr . var
是否有可能以及如何在不从类型创建对象并调用它的情况下获取类型的 reflect.Type reflect.TypeOf(obj) Java 中的内容是:MyType.class 最佳答案 您可以使用以
我是一名优秀的程序员,十分优秀!