TypeScript is a JavaScript but with an additional syntax that is called type system. All the JS rules also apply for TypeScript, so array functions, objects, arrays, spreading, everything that you know in JS can be used for writing a TypeScript.
The purpose of TypeScript is to catch errors early in the development process. To compare with JavaScript, to find a possible error or a bug, you first need to execute the code. That is not an ideal process, which makes development slower, because you need to continually rerun the code to see if you have left a bug somewhere.
With the help of the type system, during development your code is analysed continuously, looking for possible errors and or bugs. If it finds one, then you are noticed inside the code editor with a message of the error and a provided fix. And all of this happens without the need to execute the code.
TypeScript compiler analyses code by using type annotations. Type annotations let you define the type of variable, input or output for the function or method. For example, you can annotate the type of the function to be a String or some variable to be the type of a Boolean. And once you annotate, it tells the compiler that only this specific type is allowed to use. If the compiler detects a different type used on the identifier, it throws an error. In other words, you are describing the information that is going through your code.
Type annotations are used during development only. After the code is compiled from TypeScript to JavaScript, all the type system is removed. You will not see any types that you defined. And the browser or NodeJs does not understand what the TypeScript is, nor it needs to know about it. The types are used only during the development process to help catch errors fast.
Many strong typed language compilers provide an option for code optimisation. That is not the case with TypeScript. It does not do any performance optimisations during the compilation process. It just removes the type system and converts the code to the plain JavaScript.
Summary
To sum up, TypeScript is a JavaScript + Type System. It bounds types (i.e. Boolean, String or Number), to the expressions (i.e. variables, function inputs or outputs), and makes sure that only these types are used. It speeds up the development process because mistakes are caught early, before executing the code. TypeScript is used only in development, and after the compilation, the code converts to plain JavaScript getting stripped of all types.
Top comments (0)