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.

Suggestion: Allow interfaces to "implement" (vs extend) other interfaces

See original GitHub issue

Search Terms

interface, implements

Suggestion

Allow declaring that an interface “implements” another interface or interfaces, which means the compiler checks conformance, but unlike the “extends” clause, no members are inherited:

interface Foo {
  foo(): void;
}

interface Bar implements Foo {
  foo(): void; // must be present to satisfy type-checker
  bar(): void;
}

Use Cases

It is very common for one interface to be an “extension” of another, but the “extends” keyword is not a universal way to make this fact explicit in code. Because of structural typing, the fact that one interface is assignable to another is true with or without “extends,” so you might say the “extends” keyword serves primarily to inherit members, and secondarily to document and enforce the relationship between the two types. Inheriting members comes with a readability trade-off and is not always desirable, so it would be useful to be able to document and enforce the relationship between two interfaces without inheriting members.

Consider code such as:

import { GestureHandler } from './GestureHandler'
import { DropTarget } from './DropTarget'

export interface DragAndDropHandler extends GestureHandler {
  updateDropTarget(dropTarget: DropTaget): void;
}

function createDragAndDropHandler(/*...*/): DragAndDropHandler {
  //...
}

While this code is not bad, it is notable that DragAndDropHandler omits some of its members simply because it has a relationship with GestureHandler. What are those members? What if I would like to declare them explicitly, just as I would if GestureHandler didn’t exist, or if DragAndDropHandler were a class that implemented GestureHandler? I could write them in, but the compiler won’t check that I have included all of them. I could omit extends GestureHandler, but then the type-checking will happen where DragAndDropHandler is used as a GestureHandler, not where it is defined.

What I really want to do is be explicit about — and have the compiler check — that I am specifying all members of this interface, and also that it conforms to GestureHandler.

I would like to be able to write:

export interface DragAndDropHandler implements GestureHandler {
  updateDropTarget(dropTarget: DropTaget): void;
  move(gestureInfo: GestureInfo): void
  finish(gestureInfo: GestureInfo, success: boolean): void
}

Examples

See above

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript / JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. new expression-level syntax)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:49
  • Comments:27 (8 by maintainers)

github_iconTop GitHub Comments

7reactions
dragomirtitiancommented, Mar 11, 2019

@ArmorDarks I think you can achieve the effect you want without any changes to typescript. You can have a type alias as the extends clause. The type alias can check if the currently declared interface extends the wanted interface in its type parameters but the actual result of this type alias will never add anything to the interface (it will be {}).

interface A {
    foo: string;
    bar: number
}

type Implements<T, U extends T> = {}

interface B extends Implements<A, B> {
    foo: string;
    bar: number
}

Usually unused type parameters are a bad idea, but I think we can at least rely on the fact that the type parameter constrains will be checked on instantiation of the type alias even if they are not used afterwards (although I am ready to be contradicted on that by anyone with more insight into this).

The disadvantage of this is that you have to be aware that this exists and of its semantics, but ultimately using implements would have the same drawbacks and the implements solution might actually confuse a lot of people when they mistakenly use implements and get different results then they would expect.

4reactions
Alecellcommented, May 20, 2021

Maybe this is a better readable approach to reach the behavior: https://www.codegrepper.com/code-examples/typescript/typescript+override+interface+property

Based on the example above I made this, a lot readable imo despite it don’t use interfaces but types

type GenericObject = {
  [key: string]: any;
};

interface Services {
  req: GenericObject;
  res: GenericObject;
}

type Implements<T, R extends T> = R;

type MyNewService = Implements<Services, {
  req: {
    //  request type
  };
  res: {
    //  response type
  };
}>;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Suggestion: Allow interfaces to "implement" (vs extend) other ...
It is very common for one interface to be an "extension" of another, but the "extends" keyword is not a universal way to...
Read more >
Implements vs extends: When to use? What's the difference?
extends is for extending a class. implements is for implementing an interface. The difference between an interface and a regular class is that...
Read more >
Implements vs extends When to use What s the difference
Extends is for extending a class. implements are for implementing an interface. The difference between an interface and a regular class is ...
Read more >
Extends vs Implements in Java - GeeksforGeeks
1. By using “extends” keyword a class can inherit another class, or an interface can inherit other interfaces · 2. It is not...
Read more >
c# - Should interfaces extend (and in doing so inherit methods ...
First, there must be a real difference between the two interfaces, i.e., there must be instances that implement the parent interface but not...
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