Duplicate identifier error when using multiple generic interfaces with the same name

Topics: General
Jun 5, 2013 at 3:24 PM
I'm not sure if this is a bug, or just something I don't understand about TS generics. I'm trying to model an interface where there might be "n" type parameters, much like the Action<T1,T2...> classes in C#.

So I've got some interface definitions that look like this:
interface Multiple<T1>{
    t1: T1;
}

interface Multiple<T1, T2>{
    t1: T1;
    t2: T2;
}
But the compiler throws an error on the second interface: "Duplicate identifier t1".

Is that expected? It seems like the compiler is treating both Multiple<T1> and Multiple<T1,T2> as really just being the same Multiple interface. Is that correct?

I could work around it with something like this, of course:
interface Multiple<T1>{
    t1: T1;
}

interface Multiple<T1, T2>{
    t2: T2;
}
But the problem with that approach is that there's no way from me to declare a variable of just type Multiple<T1>. If I try this:

var m: Multiple<User>;

The compiler gives me the error:
error TS2090: Generic type 'Multiple<T1, T2>' requires 2 type argument(s).
Is the best way to implement this to give each interface a completely separate name, like so?
interface Multiple1<T1>{
    t1: T1;
}

interface Multiple2<T1, T2>{
    t1: T1;
    t2: T2;
}

var m1: Multiple1<User>;
var m2: Multiple2<User, Room>;
Developer
Jun 5, 2013 at 5:15 PM
TypeScript doesn't support overloading on arity like C#. Instead, TypeScript allows you to have multiple declarations for the same interface. Such declarations are merged into one by the compiler (similar to partial classes in C#). You're seeing an error because the two declarations of 't1' conflict when the interface declarations are merged. And, yes, the way to do this is to give each interface a unique name.
Jun 5, 2013 at 6:30 PM
Cool, thanks, that's what I needed to know.