EN
TypeScript - interface constructor
11 points
In TypeScript interface with a constructor can not be implemeneted by any class, but it is possible use constructor interface in following way.
xxxxxxxxxx
1
interface ISquare {
2
getName() : string;
3
4
// other method declarations...
5
}
6
7
interface ISquareConstructor {
8
new (name : string) : ISquare;
9
}
10
11
class LocalSquare implements ISquare {
12
13
public constructor(private name : string) {
14
// nothing here...
15
}
16
17
public getName() : string {
18
return this.name;
19
}
20
21
// other method definitions...
22
}
23
24
function createSquare(builder : ISquareConstructor, name : string) : ISquare {
25
// ISquareConstructor -> new (name : string) : ISquare
26
return new builder(name);
27
}
28
29
let square = createSquare(LocalSquare, 'Square');
30
31
console.log(square.getName());
32
// other method usage...
Output:
Square
Run it online here.