You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
interfaceTestInterface{setName(name: string|Date): void;setAge(years: number|null|undefined): void;}// the next line should give build-time error: // Class 'TestClass' incorrectly implements interface 'TestInterface'.classTestClassimplementsTestInterface{setName(name: string){// signature does not match interfaceif(name.slice(0,1)==='B'){console.log('Great name.');}}setAge(years: number){// signature does not match interfaceconsole.log(years.toExponential());}}letx: TestInterface=newTestClass();x.setName(newDate());// causes runtime errorx.setAge(null);// causes runtime error
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:
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: