You could try:
interface Newable<T> {
new (...args: any[]): T;
}
function decorate()
{
return function (target: Newable<Object>)
{
return class myExtendClass extends target {
public someOtherProp: string;
publicconstructor() {
this.someOtherProp = "test2";
}
}
};
};
But then you will get a new error:
// Error: Type 'typeof myExtendClass' is
// not assignable to type 'typeof Something'.
@decorate()
class Something{
public someProp: string;
publicconstructor() {
this.someProp = "test";
}
}
You can try to fix it:
function decorate()
{
return function (target: Newable<Object>)
{
class myExtendClass extends target {
public someOtherProp: string;
publicconstructor() {
this.someOtherProp = "test2";
}
}
return (<any>myExtendClass); // notice casting!
};
};
And again you will get another error:
let something = new Something();
console.log(something.someProp);
// Property 'someOtherProp' does not
//exist on type 'Something'.
console.log(something.someOtherProp);
Finally, you could solve that by casting to any:
console.log((<any>something).someOtherProp); // More casting!
Of course this is not an ideal solution. I would try do do something like the following instead:
interface Newable<T> {
new (...args: any[]): T;
}
function decorate()
{
return function (target: Newable<Object>)
{
class myExtendClass extends target {
public someOtherProp: string;
publicconstructor() {
this.someOtherProp = "test2";
}
}
return (<any>myExtendClass);
};
};
function applyDecorator<T,TDecorated>(decorator, ctr): Newable<TDecorated> {
// Decorators are just functions, you don't need @ symbol to use them :)
let decorated: Newable<TDecorated> = <any>decorator()(ctr);
}
interface ISomething {
someProp: string;
}
interface ISomethingElse extends Something {
someOtherProp: string;
}
class Something implements ISomething {
public someProp: string;
publicconstructor() {
this.someProp = "test";
}
}
let SomethingElse = applyDecorator<ISomething, ISomethingElse>(decorate, Something);
let something = new SomethingElse();
console.log(something.someProp);
console.log(something.someOtherProp);
Hope it helps :)