Generics

Topics: Language Specification
Oct 2, 2012 at 1:51 PM
Edited Oct 2, 2012 at 1:52 PM

It would be nice to have a way to describe genericity in functions, classes and interfaces.

class Cup<T> {
    public T content;
    constructor(content: T) {
        this.content = content;
    }
}
I can think of a number of scenarios when a feature like this can be useful, for example when working with arrays.
I do however realize that this might be something you are already working on or at least have plans for.
Oct 2, 2012 at 2:23 PM
Edited Oct 2, 2012 at 2:25 PM

TypeScript will eventually support generics, as per the spec [1]:

NOTE: TypeScript currently doesn’t support Generics, but we expect to
include them in the final language. Since TypeScript’s static type system has 
no run-time manifestation, Generics will be based on “type erasure” and intended
purely as a conduit for expressing parametric type
relationships in interfaces, classes, and function signatures.

[1] http://go.microsoft.com/fwlink/?LinkId=267121

Oct 2, 2012 at 2:23 PM
Edited Oct 2, 2012 at 2:24 PM

double post

Oct 2, 2012 at 2:29 PM

Didn't see that. Thanks.

Oct 2, 2012 at 5:15 PM

Speaking of Cup<T>, I wonder why the language wasn't called TScript, or better yet, TeaScript. ;)

Oct 3, 2012 at 12:13 AM

Oh hell yeah! Any chance we can see generalized abstract data types in TypeScript like we see in Haskell and F#? Algebraic data types are very expressive while maintaining high reliability. GADT syntax also allows for easy function, declarative, and object oriented programming.

Oct 3, 2012 at 12:46 PM

This would be awesome for Knockout.Js. It would reduce the amount of boilerplate interface definitions needed for Knockout.

It would be nice to have C#-style generic constraints too.

Oct 14, 2012 at 2:56 PM
Edited Oct 14, 2012 at 2:57 PM

So in need of generics! Please please make them.

Oct 14, 2012 at 3:23 PM

I find myself wondering what part of "we expect to include them in the final language" people are not understanding.

Oct 14, 2012 at 4:34 PM

When GADTs or generics are tacked-on to a language after the fact, you never get the benefits of the syntax and semantics that you would if they were part of the original design. Like OO in Perl; bless, really? Or lambdas in C++/Java.

The Free Pascal project has with been flirting with the idea of adding generics for years. It ain't gonna happen. These sorts of features either make it into the core conception, or not at all.

Oct 14, 2012 at 10:58 PM

It would be wonderful if (G)ADTs made it into TypeScript.  We now have two decades of programmers who believe OO is the best thing since sliced bread, who have never seriously been shown the alternatives.  TypeScript has a real opportunity here to advance the industry.

I really miss ADTs; OO is so blooming verbose.