-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add override / noImplicitOverride support for interfaces #45136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Although this is intentional:
|
I want to be able to override even just for documentation:
|
This would also help to notice possibly unused exported methods when an interface is removed from the |
yup this makes sense. could reuse the |
Imo it's totally fine to re-use |
on second thought, I realize why this unpopular: Perhaps we can move this to JSdocs? There is already some cross-compatibility between JSdocs and TS, maybe we can use the
class Cat implements Animal {
/** @implements Animal */
clone() { return new Cat(this.name); }
} |
Note that this is exactly how Kotlin already does this: whenever you implement a method, you'd add the |
Folks, don't forget to vote for this request! π€ Just wanted to emphasize the original poster's point that Java has had it since 2006, long before Kotlin was even a dream :D However, I love the idea of making it mandatory. In addition, I would love more type inference. For example, this doesn't work: interface ITest {
test(arg: string): number
}
class Test implements ITest {
test(arg) { // Parameter 'arg' implicitly has an 'any' type.(7006)
return arg.length
}
} While it seems to be the right (and unfortunate) thing for TypeScript, something like the |
I feel it would be a great built-in feature to improve documentation and detect correctness. (More weight on the documentation side in complex classes) |
Without this feature, it is difficult to use type guards effectively. interface ITest {
mySpecialProperty: true;
doSth(): number;
}
function isITest(obj: any): obj is ITest {
return obj?.mySpecialProperty === true;
}
class Foo implements ITest {
mySpecialProperty = true as const;
doSth() {
return 42;
}
}
const foo = new Foo();
if (isITest(foo)) {
console.log(foo.doSth());
} Eventually, ITest is removed from Foo, but the property used for the type guard is overlooked. class Foo {
mySpecialProperty = true as const;
}
const foo = new Foo();
if (isITest(foo)) {
console.log(foo.doSth()); //Error
} If we had override, this could not happen. |
I don't see any downsides to this and it makes and helps keeping the code more clean. |
Suggestion
π Search Terms
noImplicitOverride interface override
β Viability Checklist
My suggestion meets these guidelines:
β 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:If you rename
doSomething()
in the interface todoSomethingElse()
, 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 adoSomething()
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: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.
The text was updated successfully, but these errors were encountered: