Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created February 11, 2017 12:44
Show Gist options
  • Save bennadel/9e3f573616c38858238156a14dbb8eeb to your computer and use it in GitHub Desktop.
Save bennadel/9e3f573616c38858238156a14dbb8eeb to your computer and use it in GitHub Desktop.
Using ANY Type Prevents Function Parameter Type-Checking In TypeScript 2.1.5
// Passing a string to an :string argument.
logString( "I am a String" );
// Passing a Boolean to a :string argument - this will raise a type-checking error.
logString( true );
// Passing an ANY to a :string argument - this will NOT RAISE a type-checking error
// because ANY essentially "opts out" of type-checking.
// --
// Read More: https://github.com/Microsoft/TypeScript/issues/9999
logString( <any>( new Date() ) );
// ----------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------- //
function logString( value: string ) : void {
if ( value.toLowerCase ) {
console.log( value.toLowerCase() );
}
// Even if we pass-in an ANY type, opting out of parameter type checking, the
// :string type is still used to validate the parameter / value consumption
// within the function body itself. As such, this guard statement will raise a
// type-checking error because .getTime() is NOT a property of a String.
if ( value.getTime ) {
console.log( value.getTime() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment