简体   繁体   English

打字稿中的泛型类型变量约束

[英]generic type variable constraints in typescript

I am looking for the equivalent of the following c# syntax for typescript我正在寻找相当于以下 c# 语法的打字稿

public abstract class PBaseHierarchical<T> : PBase where T : PBaseHierarchical<T>
    {
...
}

I only find the constraint "extends" which would be the following in typescript:我只找到约束“扩展”,它在打字稿中如下所示:

export abstract class PBaseHierarchical<T extends PBaseHierarchical<T>> extends PBase {
...
}

But from my understanding "extends" says that T should inherit from PBaseHierarchical.但是根据我的理解,“扩展”表示 T 应该从 PBaseHierarchical 继承。 What I want is that T is of type PBaseHierarchical.我想要的是 T 是 PBaseHierarchical 类型。

Thanks a lot for any help !非常感谢您的帮助!

extends in type constraint is actually covers both:类型约束中的extends实际上涵盖了两者:

T is of type PBaseHierarchical or T "extends" PBaseHierarchical T 是 PBaseHierarchical 类型T “扩展” PBaseHierarchical

Pretty much the same as where T : PBaseHierarchical in C#.与 C# 中的 where T : PBaseHierarchical 几乎相同。

Also pay attention that types are compared structurally in Typescript.还要注意类型在 Typescript 中的结构比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM