Here is one way to approach mixins using the interfaces and static create() methods. Interfaces support multiple inheritance, so you do not need to override interfaces for your mixins, and the static create() method allows you to return an instance of Model() as an IModel ( <any> is required). to suppress the compiler warning.) You will need to duplicate all your member definitions for Model on IModel , which sucks, but it seems like this is the cleanest way to achieve what you want in the current version of TypeScript.
edit: I defined a slightly simpler approach to mixins support and even created a helper class to define them. Details can be found.
function asSettable() { this.get = function(key: string) { return this[key]; }; this.set = function(key: string, value) { this[key] = value; return this; }; } function asEventable() { this.on = function(name: string, callback) { this._events = this._events || {}; this._events[name] = callback; }; this.trigger = function(name: string) { this._events[name].call(this); } } class Model { constructor (properties = {}) { }; static create(): IModel { return <any>new Model(); } } asSettable.call(Model.prototype); asEventable.call(Model.prototype); interface ISettable { get(key: string); set(key: string, value); } interface IEvents { on(name: string, callback); trigger(name: string); } interface IModel extends ISettable, IEvents { } var x = Model.create(); x.set('foo', 'bar');
source share