TypeScript type parameter to implement multiple interfaces

Matt picture Matt · Jul 18, 2015 · Viewed 11.5k times · Source

In C#, I can do this:

class Dictionary<TKey, TVal> where TKey : IComparable, IEnumerable { }

Is there a way in TypeScript 1.5 beta for a type parameter in a generic class or function to implement multiple interfaces, without creating an entirely new interface for the purpose?

The obvious way is obviously not working due to the ambiguity of commas.

class Dictionary<TKey extends IComparable, IEnumerable, TValue> { }

By the way, funnily enough, extends can handle interface unions perfectly fine in generics:

class Dictionary<TKey extends IComparable|IEnumerable, TValue> { }

Answer

Alex picture Alex · Jan 5, 2016

Intersection types are now here since TS 1.6 and you can use it like this in your above example:

class Dictionary<TKey extends IComparable & IEnumerable, TValue> { }