This project is read-only.

Plans for Type Signature Variables ala Haskell?

Topics: General, Language Specification
Nov 8, 2012 at 10:10 PM
Edited Nov 8, 2012 at 10:30 PM

I love typescript so far. I'd like to write a function "filter" that basically does es5's filter, but curries it, so you can do something like this:

function isDead(person) {
	return (person.state == "dead");

// deadPeople would pick up its type automatically, via type inference
// var deadPeople: (people:IPerson []): IPerson [];
var deadPeople = filter(isDead);

You can current write a filter that works for this, but you have to cast it or it'll be filled with "all" instead of the real type


function filter(iterator:(item:any) => bool):any [] {
    return function(items: any[]) {
        return items.filter(iterator)

I'd like it to know the type automatically by using type variables, like this


function filter(iterator:(item:a) => bool):a [] {
    return function(items: a[]) {
        return items.filter(iterator)

Where "a" means, the type of whatever you pass in. It means that a doesn't have to be any particular type, but the types have to match. 

Nov 8, 2012 at 11:07 PM

Here's another example. It's currently impossible to define Array.prototype.filter such that the following could catch if you misspelled the property "name", even if it exists on IPlayer. The only option is to type "p", which kind of defeats the conciseness of fat arrow functions. 

function playerByName(name:string, players:IPlayer[]) {
  return players.filter((p) => (p.nam3 == name))[0] // we misspelled name and the compiler doesn't catch it

Nov 12, 2012 at 4:24 PM

Type variables (aka 'generics') are highly requested feature (  We're actively working on making this happen.


Nov 12, 2012 at 7:59 PM
Edited Nov 12, 2012 at 7:59 PM

Yes, please! JavaScript would be phenomenal if it simply copied Haskell GADTs.

Nov 12, 2012 at 9:03 PM

With generics the 'filter' function would work in much the same way as LINQ does in C#.  We would also need good support for type parameter inference.

Nov 13, 2012 at 8:03 AM

When you're actively working on this, why is the status still "proposed" and not "active"? Please involve the community more. :(