Created
November 9, 2016 15:57
-
-
Save ExpHP/0bf8144549efdc15a11bd565f29086f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Goal: make mistakes like the first function fail to compile. | |
| import * as $ from 'jquery'; | |
| function makeThisNotCompile(): number { | |
| // NOTE: With @types/jquery, | |
| // this is defined to return 'any' (and very likely, NOT a number). | |
| // The typechecker has been rendered useless, yet this looks no different | |
| // at all from any piece of code where the type checker CAN be trusted. | |
| // | |
| // --noImplicitAny does not catch this because it does not apply. | |
| return $("input").val(); | |
| } | |
| function allowThisToCompile(): number { | |
| // With an explicit type assertion, the potential for danger becomes clear. | |
| return $("input").val() as number; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment