I tried to create an instance from the base class. None of the above examples worked for me, as they needed a specific type to call the factory method.
After some research on this issue and the inability to find a solution on the Internet, I found that it works.
protected activeRow: T = {} as T;
Pieces:
activeRow: T = {} <-- activeRow now equals a new object...
...
as T; <-- As the type I specified.
Together
export abstract class GridRowEditDialogBase<T extends DataRow> extends DialogBase{ protected activeRow: T = {} as T; }
However, if you need an actual instance, you should use:
export function getInstance<T extends Object>(type: (new (...args: any[]) => T), ...args: any[]): T { return new type(...args); } export class Foo { bar() { console.log("Hello World") } } getInstance(Foo).bar();
If you have arguments, you can use.
export class Foo2 { constructor(public arg1: string, public arg2: number) { } bar() { console.log(this.arg1); console.log(this.arg2); } } getInstance(Foo, "Hello World", 2).bar();
Christian Patzer Oct 09 '17 at 10:54 on 2017-10-09 22:54
source share