question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow extending multiple interfaces with different, but compatible types

See original GitHub issue
interface Change {
  uid: string;
  type: string;
}

interface SomeChangeExtension {
  type: 'some';
  foo: number;
}

interface SomeChange extends Change, SomeChangeExtension { }

In this example, I was expecting SomeChange to have a type equivalent to:

interface SomeChange {
  uid: string;
  type: 'some';
  foo: number;
}

But it would result in error:

Interface 'SomeChange' cannot simultaneously extend types 'Change' and 'SomeChangeExtension'.
  Named property 'type' of types 'Change' and 'SomeChangeExtension' are not identical.

In this case, 'some' is compatible with string if the order of interfaces being extended is respected.

The reason why I want this to be allowed is that, I need to maintain multiple interfaces of the same kind of change during different stages: raw change, change, broadcast change. And having to duplicate part of the “extension” on every of them doesn’t look good.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:125
  • Comments:29 (10 by maintainers)

github_iconTop GitHub Comments

142reactions
viliccommented, Aug 30, 2017

@mhegazy I was thinking about respect the extensions order, and if the later one is compatible with the former one, then use the later one instead.

E.g.:

interface You {
  name: string;
  value: number;
}

interface FooYou {
  name: 'foo';
}

interface ConcreteFooYou extends You, FooYou {
  concrete: boolean;
}

In which ConcreteFooYou should be equivalent to:

interface ConcreteFooYou {
  name: 'foo';
  value: number;
  concrete: boolean;
}
24reactions
Mmokscommented, Jul 1, 2019

@vilic This should help you

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

interface Change {
  uid: string;
  type: string;
}

interface SomeChangeExtension {
  type: 'some';
  foo: number;
  amount: number;
}

interface SomeChange extends Change, Omit<SomeChangeExtension, 'foo' | 'amount'> {
 foo: string;
 amount: string;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Extend multiple interfaces in TypeScript | bobbyhadz
TypeScript is telling us we can't extend from an interface and use the same property name with a different, incompatible type.
Read more >
Extending object-like types with interfaces in TypeScript
Extending multiple interfaces refers to the concept of composition where the interface is designed to extend attributes it needs.
Read more >
Two interfaces with same methods having same signature but ...
The question is: Can a java class implement Two interfaces with same methods having the same signature but different return types??
Read more >
An interface extending multiple interfaces - java - Stack Overflow
The best thing to do is try to compile it for yourself and find out. · The two methods have conflicting return types,...
Read more >
Handbook - Interfaces - TypeScript
To describe a function type with an interface, we give the interface a call signature. This is like a function declaration with only...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found