Some Problems With Types as They Stand Currently

Topics: General, Language Specification
Oct 4, 2012 at 3:15 PM
Edited Oct 4, 2012 at 3:16 PM
  1. No generics makes it very hard to implement things like promises or create declaration files for libraries like knockout. I know it's planned...but I'm just emphasizing it's importance for the real world. When you do get around to's going to be really important the type inference works for determining generic types too.
  2. No way to designate that the type is the caller. Observe this code: 


declare module ko { 
    interface ObservableItem { 
        (value: any):caller;

    export function observable(val:any) : ObservableItem;


See caller? That should be typed based on whatever the function was invoked from. To represent ko.oberservable I think we would have to combine caller this with generics and type inference for something like this:


declare module ko { 
    interface ObservableItem<T> { 
        (value: <T>):caller;

    export function observable(val:<T>) : ObservableItem<T>;
    export function observableArray(val: <T>[]) : ObservableItem<T[]>;

var myObj = {

myObj.firstName("something else").lastName("another one");

Otherwise, things can get unusable fast. Please correct me if I'm wrong.

Oct 4, 2012 at 4:26 PM


1) We're looking into generics.  There is active discussion about them.  We're currently working out what people need there by asking for motivating examples that will help drive the design.

2) This reminds me of selfType, a way of getting a handle on the type that's implementing the interface from inside the interface.  This is related to generics, but is useful even without full generics.  You may want to add a feature request to the issue tracker about this one specifically, as I can see it getting used for describing objects that do factory-like things, for example.

Oct 4, 2012 at 7:29 PM

The second point looks like an interesting way to implement a fluent interface.

Couldn't this be done by a second type parameter? You'd have to be able to reference that type somehow.

Oct 5, 2012 at 4:31 PM

In general, this kind of facility requires some level of support for generics in the type system.