Skip to content

Instantly share code, notes, and snippets.

@ExpHP
Created November 9, 2016 15:57
Show Gist options
  • Select an option

  • Save ExpHP/0bf8144549efdc15a11bd565f29086f3 to your computer and use it in GitHub Desktop.

Select an option

Save ExpHP/0bf8144549efdc15a11bd565f29086f3 to your computer and use it in GitHub Desktop.
// 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