Please Support Generic Parameter Overloads

Topics: General, Language Specification
Aug 7, 2013 at 7:58 PM
Edited Apr 12, 2014 at 6:37 AM
I originally created an issue for this, but wanted to add it here also for discussion as a feature suggestion:

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.
Apr 11, 2014 at 11:51 PM
Any thoughts on this?
Coordinator
Apr 14, 2014 at 4:52 PM
We could potentially support this in the future.

When this first came up, we didn't require you to put the type arguments on the classes and would infer an argument of 'any'. This made them less useful and potentially pretty confusing. Now that we require you to put the type arguments explicitly on the types, it should be possible to explore adding support for these type parameter overloads. I agree, they'd be handy for configuring defaults like you show with View.
Apr 14, 2014 at 7:54 PM
Great, so I created a new work item for it:
https://typescript.codeplex.com/workitem/2438

Thanks!