i have a "compilation" problem in this case :
interface IDataObjectConstructor {
new (objJSON?: any): myDataObject;
}
class myDataObject implements IDataObjectConstructor {
constructor(objJSON: any = null) {
for (var prop in objJSON) {
this[prop] = objJSON[prop];
}
}
}
it say :
class 'myDataObject' incorrectly implements interface 'IDataObjectConstructor'.
Type 'myDataObject' provides no match for signature 'new (objJSON?: any) : myDataObject'
Finally i want to use this object like this :
class viewModelList<T extends myDataObject>{
item: T;
constructor(itemType: T) { this.item = itemType; }
itemBuilder(json?) { return new this.item(json); }
}
class derivedDataObject extends myDataObject{
constructor(objJSON: any = null) { super(objJSON); }
}
class derivedViewModelList extends viewModelList<derivedDataObject>{
constructor() { super(derivedDataObject); }
}
let oManager = new derivedViewModelList();
Benefits to using TypeScript interface constructors With a constructor on the interface, you can specify that all of your types must have certain methods/properties (normal interface compliance) but also control how the types get constructed by typing the interface like you would with any other method/property.
No, you cannot have a constructor within an interface in Java. You can have only public, static, final variables and, public, abstract, methods as of Java7. From Java8 onwards interfaces allow default methods and static methods.
A constructor is a special function of the class that is responsible for initializing the variables of the class. TypeScript defines a constructor using the constructor keyword. A constructor is a function and hence can be parameterized. The this keyword refers to the current instance of the class.
When working with classes and interfaces, it helps to keep in mind that a class has two types: the type of the static side and the type of the instance side. You may notice that if you create an interface with a construct signature and try to create a class that implements this interface you get an error:
interface ClockConstructor {
new (hour: number, minute: number);
}
class Clock implements ClockConstructor { // error
constructor(h: number, m: number) { }
}
This is because when a class implements an interface, only the instance side of the class is checked. Since the constructor sits in the static side, it is not included in this check.
Instead, you would need to work with the static side of the class directly. In this example, we define two interfaces, ClockConstructor for the constructor and ClockInterface for the instance methods. Then for convenience we define a constructor function createClock that creates instances of the type that is passed to it.
interface ClockConstructor {
new (hour: number, minute: number): ClockInterface;
}
interface ClockInterface {
tick();
}
function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface {
return new ctor(hour, minute);
}
class DigitalClock implements ClockInterface {
constructor(h: number, m: number) { }
tick() {
console.log("beep beep");
}
}
class AnalogClock implements ClockInterface {
constructor(h: number, m: number) { }
tick() {
console.log("tick tock");
}
}
let digital = createClock(DigitalClock, 12, 17);
let analog = createClock(AnalogClock, 7, 32);
Because createClock
’s first parameter is of type ClockConstructor, in createClock(AnalogClock, 7, 32)
, it checks that AnalogClock has the correct constructor signature.
See the documentation for more.
You don't need your class to implement the constructor interface, that happens automatically.
For example:
interface IDataObjectConstructor {
new (objJSON?: any): myDataObject;
}
class myDataObject {
constructor(objJSON: any = null) {
for (var prop in objJSON) {
this[prop] = objJSON[prop];
}
}
}
function factory(ctor: IDataObjectConstructor) {
return new ctor();
}
let o = factory(myDataObject);
(code in playground)
The factory
function expects an IDataObjectConstructor
and we're passing the class itself and you don't get any compilation errors even though your class isn't declared to implement it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With