Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何对接受泛型的类型使用`ConstructorParameter`实用程序?_Typescript_Typescript Typings - Fatal编程技术网

Typescript 如何对接受泛型的类型使用`ConstructorParameter`实用程序?

Typescript 如何对接受泛型的类型使用`ConstructorParameter`实用程序?,typescript,typescript-typings,Typescript,Typescript Typings,我试图从类中获取构造函数参数类型。但是,我从中提取的类接受泛型类型值 有人对如何做到这一点有什么见解吗 我遇到的错误是: ConstructorParameters<typeof (DictionaryClass<contentType>) >[0]; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Operator '>' cannot

我试图从类中获取构造函数参数类型。但是,我从中提取的类接受泛型类型值

有人对如何做到这一点有什么见解吗

我遇到的错误是:

ConstructorParameters<typeof (DictionaryClass<contentType>) >[0];
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                             Operator '>' cannot be applied to types 'pathfinder_T' and 'number[]'.
ConstructorParameters[0];
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
运算符“>”不能应用于类型“pathfinder”和“number[]”。
您可以结合使用
推断
关键字:

class Foo<T> {
    foo?: T;
}

class Bar {
    constructor(arg: Foo<string>) {
        // NOOP
    }
}

type ExtractFooType<P> = P extends Foo<infer T> ? T : never;
type foo = ExtractFooType<ConstructorParameters<typeof Bar>[0]>
class-Foo{
foo?:T;
}
分类栏{
构造函数(arg:Foo){
//努普
}
}
类型ExtractFooType

=P扩展Foo?T:从来没有; 类型foo=ExtractFooType

您可以结合使用
推断
关键字:

class Foo<T> {
    foo?: T;
}

class Bar {
    constructor(arg: Foo<string>) {
        // NOOP
    }
}

type ExtractFooType<P> = P extends Foo<infer T> ? T : never;
type foo = ExtractFooType<ConstructorParameters<typeof Bar>[0]>
class-Foo{
foo?:T;
}
分类栏{
构造函数(arg:Foo){
//努普
}
}
类型ExtractFooType

=P扩展Foo?T:从来没有; 类型foo=ExtractFooType


当前不受支持当前不受支持
常量栏=…
行不需要。@GarlefWegart哎呀。打字错误非常正确。
const bar=…
行是不需要的。@GarlefWegart哎呀。打字错误完全正确。