您可以在网上找到以下内容:
更高的kinded类型==类型构造函数?
Run Code Online (Sandbox Code Playgroud)class AClass[T]{...} // For example, class List[T]
有人说这是一种更高的kinded类型,因为它抽象了符合定义的类型.
较高的kinded类型是采用其他类型并构造新类型的类型
更高的kinded type == type构造函数,它将类型构造函数作为类型参数?
在更高级别的Generics中,您可以阅读
...抽象类型抽象的类型("更高级的类型")......"
这表明
Run Code Online (Sandbox Code Playgroud)class XClass[M[T]]{...} // or trait YTrait[N[_]]{...} // e.g. trait Functor[F[_]]
是一种更高级的类型.
因此,考虑到这一点,很难区分类型构造函数,更高的kinded类型和类型构造函数,它将类型构造函数作为类型参数,因此上面的问题.
我需要实现一个GetClassParameter<T>像这样工作的类型:
class Foo<T> {
}
type foo = Foo<string>
type x = GetClassParameter<foo>; // should be string
Run Code Online (Sandbox Code Playgroud)
很抱歉,如果它是重复的,我找不到它。我只找到了一个硬编码的解决方案(来源):
type GetFooParameter<T extends Foo<any>> = T extends Foo<infer R> ? R : unknown;
Run Code Online (Sandbox Code Playgroud)
我试图做这样的事情:
class Foo<T> {
public value: T;
public value2: string;
constructor (value: T) {
this.value = value;
}
}
type BaseT<T> = {
value: T // if removed, it wouldn't work
}
type GetClassParameter<T extends BaseT<any>> = T extends BaseT<infer R> ? R : unknown; …Run Code Online (Sandbox Code Playgroud) javascript typescript typescript-generics typescript-typings