我正在尝试获取类的实例方法的类型。除了在类的原型中查找类型之外,还有其他内置(更好)的方法吗?
class MyClass
{
private delegate: typeof MyClass.prototype.myMethod; // gets the type ( boolean ) => number;
public myMethod( arg: boolean )
{
return 3.14;
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
是否可以推断 TypeScript 中类的构造函数类型?我尝试了这个,但似乎不起作用:
type Constructor<K> = K extends { new: infer T } ? T : any;
Run Code Online (Sandbox Code Playgroud) 假设有一个容器类型,其数组属性为未知/生成类型T1,T2等等(短T*):
interface MultiContainer\n{\n Item1: T1[];\n Item2: T2[];\n ...\n}\nRun Code Online (Sandbox Code Playgroud)\n是否可以使用映射类型派生以下类型:
\ninterface SingleContainer\n{\n Item1: T1;\n Item2: T2;\n ...\n}\nRun Code Online (Sandbox Code Playgroud)\n我正在寻找一些表达方式,例如:
\ntype SingleContainer =\n { [ P in keyof MultiContainer ]: MultiContainer[P] }\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80 returns T*[] instead of T* \nRun Code Online (Sandbox Code Playgroud)\nMultiContainer[P]返回类型T*[] but I need an expression that returns T*
提前致谢!
\n考虑IDog与方法的接口likes<T extends IDog>( other: T )。该方法采用一个类型扩展接口的参数。为什么不允许在派生类中Dog使用类作为参数类型而不是接口来实现该方法?
interface IDog
{
likes<T extends IDog>( other: T ): boolean;
}
class Dog implements IDog
{
private name = "Buddy";
public likes<T extends Dog>( other: T )
// ^^^^^
// error: Property 'likes' in type 'Dog' is not
// assignable to the same property in base type 'IDog' [...]
// Property 'name' is missing in type 'IDog' but required in type 'Dog'
{
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
删除私有财产 …