Typescript Empty Brackets Interface

303 Views Asked by At

I am new to typescript, I am using the Wijimo Library and I am trying to implement the following interface

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

I have tried the following

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 { };
}
1

There are 1 best solutions below

0
MauriceNino On

Well first of all: you can't implement this interface in a class. It's a function interface.

Also, you shouldn't write return new SameClass() in a constructor as this will result in an endless loop.

An example to implement this would be:

export class SystemPageModel {
    constructor() {
    }

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


// ....


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