This post Introduction to TypeScript and How Does it Work?
first appeared on positronX.io - Learn Full Stack Development
1 What is TypeScript?
How to Setup TypeScript using NPM and Visual Studio Code?
How does TypeScript Work?
You will notice that TypeScript files come with .ts extension. Once transpiler compiles the .ts files, you will get .js files as output.
Introducing TypeScript Compilers
The compiler will throw compile errors to notify of the same. You can correct all those errors before you actually execute the code. It is an extremely important service. If you have ever worked with codes you would know that edge cases and logic errors can result in the crash of our application during runtime.
Thanks to TypeScript, we will be using the type definitions in a consistent manner throughout the code.
The compiler constitutes of various parts which assist in the fast processing of the source code. And these parts help make the code predictable.
A parser is an extremely crucial and complex component. Parser builds a data structure from the TypeScript source files and input data. And the data structure is referred to as an Abstract Syntax Tree here. The parser comes up with a structural form of the source code. It is helpful in analyzing whether we are following the grammar of the language or not. The parser checks the source code to see if it is following the correct syntax or not.
Let’s examine the working of the binder with an example. Let’s assume that we have a module and a function with the same name. With the help of a Symbol, the binder links these named declarations. It helps the type system to instill some meaning into it.
Type Checker or Type Resolver
This component generates type diagnostics, checks semantic operations and assigns types for each construct.
Pre-processor manages and resolves references amongst files. It accomplishes its goals with the help of ///<reference path=…/> or import.
You will be able to share the code segments you create on TypeScript Playground with others. Playground for barista.ts can be accessed here.
Top comments (1)
Great article! Especially the detailed description of the compiler, it’s a great way of showing right away how useful TypeScript is. I think this language deserves even more recognition than it’s already received, and that’s why recently I wrote an article on this topic as well. However, I decided to include a bit more detailed list of benefits in order to really showcase it! I hope you’ll find some time to give it a read: stxnext.com/blog/2019/08/30/typesc...