相关疑难解决方法(0)

从泛型类中的type参数创建新对象

我正在尝试在我的泛型类中创建一个类型参数的新对象.在我的类中View,我有2个作为类型参数传递的泛型类型的对象列表,但是当我尝试制作时new TGridView(),TypeScript说:

找不到符号'TGridView

这是代码:

module AppFW {
    // Represents a view
    export class View<TFormView extends FormView, TGridView extends GridView> {
        // The list of forms 
        public Forms: { [idForm: string]: TFormView; } = {};

        // The list of grids
        public Grids: { [idForm: string]: TGridView; } = {};

        public AddForm(formElement: HTMLFormElement, dataModel: any, submitFunction?: (e: SubmitFormViewEvent) => boolean): FormView {
            var newForm: TFormView = new TFormView(formElement, dataModel, submitFunction);
            this.Forms[formElement.id] = newForm;
            return newForm;
        }

        public AddGrid(element: …
Run Code Online (Sandbox Code Playgroud)

generics typescript

121
推荐指数
6
解决办法
11万
查看次数

从TypeScript中的泛型类型获取构造函数/实例

我试图从泛型类型创建实例T,例如,

class Factory {
    public static generate<T>(): T { 
      return new T();
  }
}
Run Code Online (Sandbox Code Playgroud)

但由于T只是一种类型而不是构造函数,我们不能这样做.

是否无法在TypeScript中从泛型类型创建实例?

我也读过这些文章,但找不到解决方案.

generics typescript

9
推荐指数
1
解决办法
5830
查看次数

标签 统计

generics ×2

typescript ×2