Type Annotations
Type Annotations are an optional feature, which allows us to check and express our intent in applications e.g they indicate what types of values you can pass to the function. Type annotations (and other TypeScripts features) are enforced at compile time.

Let's take a function:
function personDetails(name: string , age: number ) {
    return "My name is " + name + " and I'm " + age;
}

var contentElement = document.getElementById('content');
contentElement.innerHTML = personDetails( "M.", 27);
As you can notice, the type annotations are expressed as part of the function parameters. The name parameter is designed as a string value and age is numeric value.
If you pass any other types of values to these parameters, the compiler will give you a compile error. This is very helpful when it comes to large applications.

Let's see what will happen when we purposely pass a string for the age parameter.
var contentElement = document.getElementById('content');
contentElement.innerHTML = personDetails( "M.", "27" );
Compilation of this file shows us the following error:
Despite this error, the compiler generated app.js file. We can say that the compiler just warn us of potential issues.

×
×