我目前正试图为一般化类别名。我使用的是typescript-collections包,想要重命名。
class Bar<T> {}
class Foo = Bar<X>;
我将如何进行第二行?
答案 0 :(得分:2)
如果您只需要在其他地方的类型注释/提示中对Bar<X>
进行别名(例如。function blah(foo: Bar<X>)
),则可以使用以下语句:
type Foo = Bar<X>;
请注意,您仍然无法执行new Foo()
,type
语句只对TypeScript的“types”部分有贡献,而对“values”部分没有贡献(就像类一样)。
如果你想要一个真正的类,一个简单而有效的解决方案是扩展原来的Bar<X>
类,如下所示:
class Foo extends Bar<X> {}
我个人并不特别喜欢这个解决方案,因为它会使你的代码不那么清晰。
答案 1 :(得分:1)
您可以从Foo
继承Bar<X>
以创建预定义X
类型参数的类:
class Foo extends Bar<X> {}
答案 2 :(得分:1)
您可以使用类型别名@toskv建议:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("Category")]
public partial class Category
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Category()
{
Product_Category_Mapping = new HashSet<Product_Category_Mapping>();
Discounts = new HashSet<Discount>();
}
public int Id { get; set; }
[Required]
[StringLength(400)]
public string Name { get; set; }
public string Description { get; set; }
public int CategoryTemplateId { get; set; }
[StringLength(400)]
public string MetaKeywords { get; set; }
public string MetaDescription { get; set; }
[StringLength(400)]
public string MetaTitle { get; set; }
public int ParentCategoryId { get; set; }
public int PictureId { get; set; }
public int PageSize { get; set; }
public bool AllowCustomersToSelectPageSize { get; set; }
[StringLength(200)]
public string PageSizeOptions { get; set; }
[StringLength(400)]
public string PriceRanges { get; set; }
public bool ShowOnHomePage { get; set; }
public bool IncludeInTopMenu { get; set; }
public bool HasDiscountsApplied { get; set; }
public bool SubjectToAcl { get; set; }
public bool LimitedToStores { get; set; }
public bool Published { get; set; }
public bool Deleted { get; set; }
public int DisplayOrder { get; set; }
public DateTime CreatedOnUtc { get; set; }
public DateTime UpdatedOnUtc { get; set; }
然后您可以将其用作类型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("Discount")]
public partial class Discount
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Discount()
{
DiscountRequirements = new HashSet<DiscountRequirement>();
DiscountUsageHistories = new HashSet<DiscountUsageHistory>();
Categories = new HashSet<Category>();
Products = new HashSet<Product>();
}
public int Id { get; set; }
[Required]
[StringLength(200)]
public string Name { get; set; }
public int DiscountTypeId { get; set; }
public bool UsePercentage { get; set; }
public decimal DiscountPercentage { get; set; }
public decimal DiscountAmount { get; set; }
public DateTime? StartDateUtc { get; set; }
public DateTime? EndDateUtc { get; set; }
public bool RequiresCouponCode { get; set; }
[StringLength(100)]
public string CouponCode { get; set; }
public int DiscountLimitationId { get; set; }
public int LimitationTimes { get; set; }
public int? MaximumDiscountedQuantity { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<DiscountRequirement> DiscountRequirements { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<DiscountUsageHistory> DiscountUsageHistories { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Category> Categories { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Product> Products { get; set; }
}
如果您有一个接收using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class Product_Category_Mapping
{
public int Id { get; set; }
public int ProductId { get; set; }
public int CategoryId { get; set; }
public bool IsFeaturedProduct { get; set; }
public int DisplayOrder { get; set; }
public virtual Category Category { get; set; }
public virtual Product Product { get; set; }
}
实例的构造函数,那么编译器将推断出类型:
type Foo = Bar<X>;
如果您想要一个绑定到特定通用约束的新类,那么您需要执行以下操作:
let foo: Foo = new Bar<X>();
答案 3 :(得分:1)
如果您只需要一个类型别名,那么您可以使用type来创建它:
type Foo = Bar<X>;
通过这种方式,您可以执行以下操作:
let x: Foo;
但不是:
let x= new Foo();
另一个解决方案是扩展Bar并创建一个空类。并不是你应该如何使用类层次结构,但它可以满足您的需求。
class Foos extends Bar<number> { }