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哎呀。打字错误完全正确。