-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Union types confound interface implementation checks #20983
Comments
It compiles without error because parameters of methods are checked bivariantly. If you change the interface declaration to use property syntax interface TestInterface {
setName: (name: string | Date) => void;
setAge: (years: number | null | undefined) => void;
} and compile with |
Thank you for the explanation and the work-around. Much appreciated. I do hope that you will continue to explore the stricter mode of --strictFunctionTypes that includes methods. I'm sure that you can appreciate that co/contra/bi-variance is a complex topic that is best left to the compiler to figure out and tell me when I've made a mistake--meaning that most devs are not thinking about these nuances when coding. My example was meant to demonstrate how easy it is to fall into this particular trap without realizing it and running into a run-time failure later. |
Why does this compile without error?
TypeScript Version: 2.6.1
Code
Expected behavior:
Should get a build-time error: Class 'TestClass' incorrectly implements interface 'TestInterface'.
Actual behavior:
No build-time error, and a run-time error later.
The text was updated successfully, but these errors were encountered: