Like any other language, TypeScript was designed to achieve some goals. TypeScript has two goals:
To achieve these goals consider following.
Type systems improve your code by making it easier to read and by enforcing a certain degree of correctness at compile time.
.js file can be used as .ts file and TypeScript will results equivalent valid
In TypeScript, type can be implicit and provide flexibility to improve productivity during code development. For example, in the following example TypeScript will know that
Age is of type
number. But it will give an error if you assign
string value to it:
var Age = 23; Age = '456'; // Error: cannot assign 'string' to 'number'
In TypeScript, type can be explicit and allow programmers to accurately express the type that you are expecting and pre-validated by the TypeScript compiler. TypeScript uses postfix type annotations to specify type as it is popular in other optionally annotated languages.
var Age: number = 123; var Age: number = '123'; // Error: cannot assign 'string' to 'number'
In TypeScript, type can be structural and introduced through class and interface declarations and are referenced by the name given to them in their declarations. This means that duck typing is a first class language construct. Consider the following example.
$('.awesome').show(); // Error: cannot find name '$' declare var $:any; $('.awesome').show(); // Now okay!