【问题标题】:Typescript Empty Brackets InterfaceTypescript 空括号界面
【发布时间】:2020-11-19 02:16:04
【问题描述】:

我是打字稿的新手,我正在使用 Wijimo 库,我正在尝试实现以下接口

interface IItemCreator<T = any> {
  (): T;
}

我已经尝试了以下

export class SystemPageModel implements wijmo.collections.IItemCreator<SystemPageModel> {

    constructor(): SystemPageModel {
        return new SystemPageModel();
    }

    public PK: Guid;
    public Name: string;
    public SystemName: string;
    public SystemTypePK: string;    

    public ff(): SystemPageModel { };
}

【问题讨论】:

标签: typescript wijmo


【解决方案1】:

首先:你不能在一个类中实现这个接口。是一个函数接口。

另外,你不应该在构造函数中写return new SameClass(),因为这会导致无限循环。

实现这一点的一个例子是:

export class SystemPageModel {
    constructor() {
    }

    public PK: Guid;
    public Name: string;
    public SystemName: string;
    public SystemTypePK: string;
}


// ....


const systemPageModelCreator: IItemCreator<SystemPageModel> = (): SystemPageModel => {
    return new SystemPageModel();
};

【讨论】:

    猜你喜欢
    • 2016-07-05
    • 1970-01-01
    • 2012-09-27
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 2017-12-02
    • 2018-02-07
    相关资源
    最近更新 更多