小编Mar*_*uch的帖子

TypeScript:获取类的实例方法的类型

我正在尝试获取类的实例方法的类型。除了在类的原型中查找类型之外,还有其他内置(更好)的方法吗?

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

13
推荐指数
4
解决办法
3857
查看次数

在 TypeScript 中推断构造函数类型

是否可以推断 TypeScript 中类的构造函数类型?我尝试了这个,但似乎不起作用:

type Constructor<K> = K extends { new: infer T } ? T : any;
Run Code Online (Sandbox Code Playgroud)

typescript conditional-types

7
推荐指数
1
解决办法
5886
查看次数

TypeScript 映射类型:获取数组的元素类型

假设有一个容器类型,其数组属性为未知/生成类型T1T2等等(短T*):

\n
interface MultiContainer\n{\n    Item1: T1[];\n    Item2: T2[];\n    ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n

是否可以使用映射类型派生以下类型:

\n
interface SingleContainer\n{\n    Item1: T1;\n    Item2: T2;\n    ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我正在寻找一些表达方式,例如:

\n
type 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*  \n
Run Code Online (Sandbox Code Playgroud)\n

MultiContainer[P]返回类型T*[] but I need an expression that returns T*

\n

提前致谢!

\n

typescript mapped-types

5
推荐指数
1
解决办法
4429
查看次数

为什么实现接口的类中的泛型成员函数不能采用类(而不是接口)类型的参数?

考虑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)

删除私有财产 …

typescript typescript-types

5
推荐指数
1
解决办法
61
查看次数