TypeScript is a language that is transpiled to JavaScript; that is, TypeScript code is processed to generate JavaScript code.
As a first approximation, you may imagine that TypeScript code is obtained by adding type declarations of variables to usual JavaScript code. For instance, let's consider this JavaScript code:
var firstName = "Francesco";
var surName = "Abbruzzese";
function fullName(x, y, spaces){
x + Array(spaces+1).join(' ') + y;
}
>fullName(firstName, surName, 3)
>"Francesco Abbruzzese"
TypeScript adds a type declaration to the firstName and surName variable declarations, and it also adds types to the fullName function arguments, and to the function's return value:
var firstName: string = "Francesco";
var surName: string = "Abbruzzese";
function fullName(x: string, y: string, spaces: number): string{
x + Array(spaces+1).join(' ') + y;
}
TypeScript and JavaScript code are very similar, the only difference being the colon followed by the type immediately after each variable or argument declaration and each function declaration.
In this very simple example, the JavaScript code generated by TypeScript transpilation is identical to the code written directly in JavaScript, so what is the advantage of using TypeScript?
Simple: type checking! The TypeScript compiler verifies type compatibility, thus immediately discovering errors that might otherwise manifest themselves with strange behaviors at runtime.
Suppose, for instance, that we call fullName with its arguments in the wrong order:
fullName(3, firstName, surName)
The TypeScript transpiler immediately discovers the error since 3 is not a string and surName is not a number, while JavaScript tries to automatically convert types and gets the wrong result:
>fullName(3, firstName, surName)
>"3Francesco"
TypeScript types are used just to perform compile-time checks, and do not influence JavaScript code generated by the transpiler. In a few words, types disappear completely in the transpiled code. This is by design, because TypeScript was conceived to maintain the same JavaScript semantics and principles while helping with compile-time checks.