1

I'm probably over thinking this. Basically, I would like to have an non-generic interface that implements a few properties, then create a second type that implements that interface with it's own generic property.

interface Car {
  model: string;
}

interface Car<T> extends Car {
  transmission: T;
}

TypeScript errors:

"All declarations of 'Car' must have identical type parameters."

The only way I can find is to name the non-generic interface something different, like CarBase. Suggestions?

3
  • What are you trying to do? Why would you separate it into two different interfaces with the same name? It's equivalent to interface Car<T> { model: string; transmission: T } Commented Feb 21, 2017 at 14:04
  • I want other code to use the non-generic reference. Currenly Im doing Car<any> to bypass the type checking, when I just want to use Car. Commented Feb 21, 2017 at 14:08
  • Why do they have to have the same name? Commented Feb 21, 2017 at 14:30

1 Answer 1

1

You can use a type alias to save you the trouble of writing Car<any>:

interface Car<T> {
    model: string;
    transmission: T;
}

type AnyCar = Car<any>;
Sign up to request clarification or add additional context in comments.

2 Comments

Is this just the same as writing: interface CarBase implements Car<any> ?
It's pretty much the same, but there are differences between interfaces and type aliases. It is explained in Interfaces vs. Type Aliases

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.