-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Bug: Interface implementations allow optional properties to be required #56321
Comments
Per https://github.com/microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs :
To make your expected behavior, you should declare interface IGreeter {
greet: (name?: string) => void;
} |
That doesn't explain why it works for I see that PR is merged in 2017 and the whole ecosystem got way more compatible with Typescript and strict typing, so it might be a good moment to re-evaluate and see if this change would still break a lot of code. |
No, it doesn't work for interface IGreeter {
greet(name: string | undefined): void;
}
class Greeter implements IGreeter {
// This incorrectly implement the interface, since it assumes it will always get a name
greet(name: string): void {
console.log(`Hello dear ${name.concat('')}`);
}
}
// No compiler error because the method will accept undefined
const greeterInterface: IGreeter = new Greeter();
greeterInterface.greet(undefined);
//But you will get runtime error: Cannot read properties of undefined (reading 'concat') |
Ah yes you're right, it only throws a compiler error if you call it like this (playground): const greeterInterface: IGreeter = new Greeter();
greeterInterface.greet(); Which is also odd. Either way I still think this is an important bug, curious what the maintainers think since I also can find ways to work around it. |
Well, this is another issue which is being discussed at #12400. At the moment, |
But it's not a bug. It's very much intentional, although unfortunate. The FAQ provides slightly more information: https://github.com/microsoft/TypeScript/wiki/FAQ#why-are-function-parameters-bivariant |
Also from #18654:
Yes, code in the wild has gotten more |
Nothing has changed regarding the underlying problem here (arrays need to be covariant so that you can ever make one), so I don't see what a re-evaluation would do. |
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
Interface optional properties required
π Version & Regression Information
Bug exists for a long time, so no big new change for that here but I couldn't find a previous issue that discussed it.
β― Playground Link
https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgJIHEoQpKyDeAUMsgOZY4AUIcAthAPwBcyAzmFKKQJQsBuAe2AATANyEAvoUIIANnFatkmbLmTBaAB1kR64JRgpqiJAPSnkAFQAWwJaAQCoWBGFkBPdVp16wyMNYooLjwSAA0bA5BfgqsAK709n4A7sCysshwsslw7kqkOJnINPTEZEbUdBAs7JwgPPxCwgRlJI4grAI6AHSyAqSUAAYAEhDpAsjCEHB4ACT4JRASg9ziJFJShObIAHIT0FBOyABGEAhwcawoAUHg0KEowgIQrCAA5H5YAI5xwFhFfCycWuE1OyE0sQgwhkAg6fnIqmgqDusEQ1TQKhw0GQAF5ihBksojNBKKtCAisVBkSE0d0KWBSeIthYAKLOI6nc6Xa6BZByWKZVxxLIeZDfX5YJRwZCA2TAmFw8qIqAAYXkilx+MJmNwjPJxNV6tYdIqqyAA
π» Code
π Actual behavior
It allows you to implement the
IGreeter
interface incorrectly by requiring a parameter in a method which is optional according to the interface.π Expected behavior
An error would be expected in the
Greeter
class that indicates that theIGreeter
interface is not correctly implemented.Additional information about the issue
For strict mode, I can't think of a reason why this isn't throwing an error. It does throw an error if you define the property in the interface as
| undefined
. There is a similar issue for interface properties, wherepublic str?: string;
doesn't require it to be implemented in the class butpublic str: string | undefined
does.The text was updated successfully, but these errors were encountered: