Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 流类型:如何类型动态类工厂_Javascript_Reactjs_Flowtype - Fatal编程技术网

Javascript 流类型:如何类型动态类工厂

Javascript 流类型:如何类型动态类工厂,javascript,reactjs,flowtype,Javascript,Reactjs,Flowtype,我为这个模糊的标题提前道歉,但我不得不简明扼要地描述我的问题 我有一个类表,它接受一个动态的行类,它将包装从类方法getRow()返回的行。我的问题是,如果不丢失从getRow()返回的类型,我就无法让它在流中工作 以下是我目前掌握的情况: // @flow class Row { constructor() {} }; class RowA extends Row {}; class RowB extends Row {}; class Table<RowType: Clas

我为这个模糊的标题提前道歉,但我不得不简明扼要地描述我的问题

我有一个类
,它接受一个动态的
类,它将包装从类方法
getRow()
返回的行。我的问题是,如果不丢失从
getRow()
返回的类型,我就无法让它在流中工作

以下是我目前掌握的情况:

// @flow

class Row {
    constructor() {}
};
class RowA extends Row {};
class RowB extends Row {};

class Table<RowType: Class<*>> {
    Row: RowType;

    constructor(RowClass: RowType = Row) {
        this.Row = RowClass;
    }

    getRow() {
        return new this.Row();
    }
};

const a = new Table(RowA);
(a.getRow(): RowA);

const b = new Table(RowB);
(b.getRow(): RowB);

const c = new Table();
(c.getRow(): Row);

必须使用声明的构造函数实现接口,或者使用(预定义的)构造函数扩展类。
行接口
类行
类表
的模板参数中可互换

我还向
getRow()
方法添加了类型。另请参见替代初始化(您不需要强制转换):

const a=新表(RowA);
设rA:RowA=a.getRow();
完整代码:

// @flow

interface RowInterface {
    constructor():void;
}

class Row implements RowInterface {
    constructor(){}
};

class RowA extends Row {
};
class RowB extends Row {
};

// bound to extend Row
//class Table<Entity: Row> { 
class Table<Entity: RowInterface> {
    _rowConstructor: Class<Entity>;

    // take a constructor
    constructor(row: Class<Entity>) {
        this._rowConstructor = row;
    }

    // return a constructed entity
    getRow(): Entity {
        return new this._rowConstructor();
    }
};


const a = new Table<RowA>(RowA);
let rA : RowA = a.getRow();

const b = new Table<RowB>(RowB);
(b.getRow(): RowB);
/@flow
接口行接口{
构造函数():void;
}
类行实现RowInterface{
构造函数(){}
};
类RowA扩展了行{
};
类RowB扩展了行{
};
//绑定到扩展行
//类表{
类表{
_行构造函数:类;
//以构造器为例
构造函数(行:类){
这个。_rowConstructor=row;
}
//返回构造的实体
getRow():实体{
返回这个新的构造函数。_rowConstructor();
}
};
常数a=新表(RowA);
let rA:RowA=a.getRow();
常数b=新表(行b);
(b.getRow():行);

如果将
作为默认的
\u行构造函数
,您将如何进行此操作?示例:
const a = new Table<RowA>(RowA);
let rA : RowA = a.getRow();
// @flow

interface RowInterface {
    constructor():void;
}

class Row implements RowInterface {
    constructor(){}
};

class RowA extends Row {
};
class RowB extends Row {
};

// bound to extend Row
//class Table<Entity: Row> { 
class Table<Entity: RowInterface> {
    _rowConstructor: Class<Entity>;

    // take a constructor
    constructor(row: Class<Entity>) {
        this._rowConstructor = row;
    }

    // return a constructed entity
    getRow(): Entity {
        return new this._rowConstructor();
    }
};


const a = new Table<RowA>(RowA);
let rA : RowA = a.getRow();

const b = new Table<RowB>(RowB);
(b.getRow(): RowB);