9

This obviously works fine in C# but not typescript, is there a way around it or will I just have to use different names?

export interface IThing<T> extends IThing {
 Value: T;
}

export interface IThing {
Name?: string;
ValueStr?: string;
Type?: string;
}

I get 'All declarations of IThing must have identical type parameters'

Apologies if I'm being dense!

1
  • @maccettura fair point Commented Feb 15, 2018 at 15:35

2 Answers 2

11

You need to use different names, only name matters when identifying the interface not the number of generic type parameters.

Generics are erased at compile time in Typescript, so there is no way to distinguish between different instantiations of a generic class. Although interfaces are only a compile time construct and could possibly have been differentiated by name as well as number of generic parameters, my guess is that since classes work this way it was decided to make interfaces work in the same way.

One work around would be to use a default type argument for the interface:

export interface IThing<T = any> {
    Name?: string;
    ValueStr?: string;
    Type?: string; 
    Value: T;
}

var d: IThing;
var foo: IThing<number> 
Sign up to request clarification or add additional context in comments.

6 Comments

You can actually, this is called declaration merging, but the name and the type parameters have to match.
@Tao, well if they are merged, then it's not different interfaces :) The question is if you can have both an interface with a generic parameter and one without, to distinct interfaces as you can in C#.
Ah ok, I didn't get the origin of the question then since I don't know much about C#. :) Also, the extends wasn't there when I posted my answer so it looked as if he tried to merge them.
This isn't right. All types are erased during compilation, not just generics. Thinking otherwise will cause grief.
@AluanHaddad true, all type annotations are erased, but you can distinguish between two classes based on their constructor, and using instanceof will distinguish between two classes. But nothing will distinguish between two different instantiations of the same generic class
|
0

Edit 2

I misunderstood the question so this isn't a correct answer. I'll leave this here to show how declaration merging can be used.


The first declaration has a type parameter T while the second doesn't. Just make sure they have both the type parameter.

export interface IThing<T> {
    Name?: string;
    ValueStr?: string;
    Type?: string;
}

Edit

interface IThing<T> extends IThing

This won't work, as I said above just make sure both interface declarations have the same name and the same type parameters

interface IThing<T>

Playground Demo

1 Comment

Declaration merging is actually one of the reasons it cannot be allowed. With defaulted type parameters it would be ambiguous whether you were trying to extend or merge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.