一种解决方案是不使用 typescript 类系统,而只使用类型和接口的系统,以及关键字“new”。
//the function that create class
function Class(construct : Function, proto : Object, ...mixins : Function[]) : Function {
//...
return function(){};
}
module Test {
//the type of A
export interface IA {
a(str1 : string) : void;
}
//the class A
//<new () => IA> === cast to an anonyme function constructor that create an object of type IA,
// the signature of the constructor is placed here, but refactoring should not work
//Class(<IA> { === cast an anonyme object with the signature of IA (for refactoring, but the rename IDE method not work )
export var A = <new () => IA> Class(
//the constructor with the same signature that the cast just above
function() { } ,
<IA> {
//!! the IDE does not check that the object implement all members of the interface, but create an error if an membre is not in the interface
a : function(str : string){}
}
);
//the type of B
export interface IB {
b() : void;
}
//the implementation of IB
export class B implements IB {
b() { }
}
//the type of C
export interface IC extends IA, IB{
c() : void;
mystring: string;
}
//the implementation of IC
export var C = <new (mystring : string) => IC> Class(
//public key word not work
function(mystring : string) {
//problem with 'this', doesn't reference an object of type IC, why??
//but google compiler replace self by this !!
var self = (<IC> this);
self.mystring = mystring;
} ,
<IC> {
c : function (){},
//override a , and call the inherited method
a: function (str: string) {
(<IA> A.prototype).a.call(null, 5);//problem with call and apply, signature of call and apply are static, but should be dynamic
//so, the 'Class' function must create an method for that
(<IA> this.$super(A)).a('');
}
},
//mixins
A, B
);
}
var c = new Test.C('');
c.a('');
c.b();
c.c();
c.d();//ok error !