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.

Add override / noImplicitOverride support for interfaces

See original GitHub issue

Suggestion

🔍 Search Terms

noImplicitOverride interface override

✅ Viability 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. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

⭐ Suggestion

📃 Motivating Example

The new override keyword in TS4.3 works for classes but not for interfaces. Would be great to have it also available for interfaces. As interfaces are technically not overridden, alternatively a new keyword like implement could be introduced (Java, for example, uses override for classes and interfaces though)

💻 Use Cases

What is the advantage of override / noImplicitOverride for interfaces? Consider this case:

interface ISomething {
  doSomething?: () => void;
}

class Something implements ISomething {
  doSomething: () => {console.log("Something")}
}

If you rename doSomething() in the interface to doSomethingElse(), this would not throw an TS error, since it is optional and therefore doesn’t have to be implemented. The implementing class would still have a doSomething() method which never gets executed.

If I would be forced to do something like this, TS could throw an error if I rename doSomething in the interface:

class Something implements ISomething {
  override doSomething: () => {console.log("Something")}
}

Currently I am using a custom TSLint rule via eslint but TSLint is already deprecated and also the rule stopped working with TS 4.3. An according ESLint is not planned for this. Anyhow, I think this check could be done directly in TS.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:12
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
HaraldKicommented, Dec 29, 2021

This would also help to notice possibly unused exported methods when an interface is removed from the implements clause and the method is no longer necessary.

4reactions
VsevolodGolovanovcommented, Aug 6, 2021

I want to be able to override even just for documentation:

interface A {
	/**
	 * Bla-bla.
	 */
	doStuff: () => void;
}

interface B extends A {
	/**
	 * Bla-bla, but in the context of B: additional concerns and ramifications etc.
	 */
	override doStuff: () => void;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

TSConfig Option: noImplicitOverride - TypeScript
How this setting affects your build.
Read more >
override and noImplicitOverride for TypeScript class inheritance
TypeScript supports the override modifier and even added a new compiler flag for enforcing reliable class inheritance! … Show more.
Read more >
Introducing Typescript `override` keyword - DEV Community ‍ ‍
As the top quote explains, the override keyword will assert that the function it describes is present in the parent class. This will...
Read more >
How can i avoid a function to be overridden in the Child Class ...
Typescript 4.3 adds a new compiler option called noImplicitOverride in this PR. This makes it an error to mistakenly override class methods ...
Read more >
Announcing TypeScript 4.3 - Microsoft Developer Blogs
In fact, we've added syntax to interfaces/object types to support ... why TypeScript 4.3 also provides a new --noImplicitOverride flag.
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