typescript类继承和带对象表示法的实例化

时间:2017-07-11 14:04:03

标签: typescript inheritance ecmascript-6

我有一个项目类,它扩展了一个abscract基础类

export abstract class Basic {
    _id?: string;
    title?: string;
    titleAbv?: string;
    description?: string;

    startDate?: number | Date;
    endDate?: number | Date;
    duration?: number | Date;
    deadline?: number | Date;

    constructor(newBasic: BasicInterface) {
        this._id = newBasic._id || null;
        this.title = newBasic.title || null;
        this.titleAbv = newBasic.titleAbv || null;
        this.description = newBasic.description || null;

        this.startDate = newBasic.startDate || null;
        this.endDate = newBasic.endDate || null;
        this.duration = newBasic.duration || null;
        this.deadline = newBasic.deadline || null;
    }
}

export interface BasicInterface {
    _id?:string;
    title?: string;
    titleAbv?: string;
    description?: string;

    startDate?: number | Date;
    endDate?: number | Date;
    duration?: number | Date;
    deadline?: number | Date;
}

export class Project extends Basic{

    color?:number;
    clientId?:string;
    clientName?:string;

    constructor(newProject:ProjectInterface){
        super(newProject.basic);
        this.color = newProject.color || 0;
        this.clientId = newProject.clientId || null;
        this.clientName = newProject.clientName || null;
    }
}

export interface ProjectInterface{
    basic:Basic;
    color?:number;
    clientId?:string;
    clientName?:string;
}

我正在使用对象表示法进行实例化,以便输入顺序无关紧要。但是我需要用

实例化
new Project ({basic:{}})

如果我扩展到Project,它将看起来像

new ObjectExample ( { project:{basic:{}}})

这不是很好,我想知道是否有一种方法可以继承类并保留对象表示法?

谢谢!

0 个答案:

没有答案