如何在typescript中的泛型类中的type参数中创建一个新对象? [英] How to create a new object from type parameter in generic class in typescript?

查看:1507
本文介绍了如何在typescript中的泛型类中的type参数中创建一个新对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在泛型类中创建一个类型参数的新对象。
在我的类View中,我有2个作为类型参数传递的泛型类型的对象列表,但是当我尝试使新的TGridView() typescript 找不到符号'TGridView'...



这是代码:

  module AppFW {
//代表一个视图
export class View< TFormView extends FormView,TGridView extends GridView> {
//表格列表
public Forms:{[idForm:string]:TFormView; } = {};

//网格列表
public Grids:{[idForm:string]:TGridView; } = {};
$ b $ public AddForm(formElement:HTMLFormElement,dataModel:any,submitFunction ?:(e:SubmitFormViewEvent)=> boolean):FormView {
var newForm:TFormView = new TFormView(formElement,dataModel ,submitFunction);
this.Forms [formElement.id] = newForm;
返回newForm;

$ b $ public AddGrid(element:HTMLDivElement,gridOptions:any):GridView {
var newGrid:TGridView = new TGridView(element,gridOptions);
this.Grids [element.id] = newGrid;
返回newGrid;



$ $ $
$ b我可以从一个通用类型?

解决方案

因为编译后的JavaScript会删除所有类型信息,所以不能使用



您可以通过将类型传递给构造函数以非泛型方式来完成此操作。

>

  class TestOne {
hi(){
alert('Hi');



class TestTwo {
构造函数(private testType){

}
getNew(){
返回new this.testType();
}
}

var test = new TestTwo(TestOne);

var example = test.getNew();
example.hi();

你可以使用泛型来扩展这个例子来收紧类型:

  class TestBase {
hi(){
alert('Hi from base');



class TestSub extends TestBase {
hi(){
alert('Hi from sub');
}
}

class TestTwo< T扩展TestBase> {
constructor(private testType:new()=> T){
}

getNew():T {
return new this.testType();
}
}

// var test = new TestTwo< TestBase>(TestBase);
var test = new TestTwo< TestSub>(TestSub);

var example = test.getNew();
example.hi();


I'm trying to create a new object of a type parameter in my generic class. In my class "View" I have 2 list of objects of generic type passed as type parameters, but when I try to make new TGridView() typescript says Could not find symbol 'TGridView'...

This is the code:

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: HTMLDivElement, gridOptions: any): GridView {
            var newGrid: TGridView = new TGridView(element, gridOptions);
            this.Grids[element.id] = newGrid;
            return newGrid;
        }
    }
}

Can I create objects from a generic type?

解决方案

Because the compiled JavaScript has all the type information erased, you can't use T to new up an object.

You can do this in a non-generic way by passing the type into the constructor.

class TestOne {
    hi() {
        alert('Hi');
    }
}

class TestTwo {
    constructor(private testType) {

    }
    getNew() {
        return new this.testType();
    }
}

var test = new TestTwo(TestOne);

var example = test.getNew();
example.hi();

You could extend this example using generics to tighten up the types:

class TestBase {
    hi() {
        alert('Hi from base');
    }
}

class TestSub extends TestBase {
    hi() {
        alert('Hi from sub');
    }
}

class TestTwo<T extends TestBase> {
    constructor(private testType: new () => T) {
    }

    getNew() : T {
        return new this.testType();
    }
}

//var test = new TestTwo<TestBase>(TestBase);
var test = new TestTwo<TestSub>(TestSub);

var example = test.getNew();
example.hi();

这篇关于如何在typescript中的泛型类中的type参数中创建一个新对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆