Closed
Description
Suggestion moved over from codeplex.
The suggestion is to allow generic parameter overloads like the following:
declare module Backbone {
class Model{}
class Events{}
class ViewOptions<TModel extends Model> {}
class Collection<TModel extends Model> {}
// Generic parameter overload
class View extends View<Backbone.Model>;
class View<TModel extends Model> extends Events { // Don't take as duplicate identifier
constructor(options?: ViewOptions<TModel>);
model: TModel;
collection: Collection<TModel>;
}
}
// With Generics Parameter overloads, old non-generic consumer
// code works as before and does not need to change.
class MyView extends Backbone.View {
}
var myView = new MyView();
var model = myView.model; // model is of type Backbone.Model
// Can gradually introduce generic parameters to Views
class DerivedModel extends Backbone.Model {
}
var myView2 = new Backbone.View<DerivedModel>();
var model2 = myView2.model; // model2 is of type DerivedModel
This is important because this would allow people to introduce generics in their library declarations without breaking large amounts of existing code for the consumers. We do this a lot in C#, and it is compatible with TypeScript design because it won't make any changes in the resulting javascript, and works pretty much like method overloads in TypeScript.
Related discussion: https://typescript.codeplex.com/discussions/452741