Introduction to TypeScript

Typescript is an open source object oriented programming language developed and maintained by Microsoft Corporation. Typescript is a strongly typed language and its first version was introduced in 2012. It is a strict super set of javascript can be implemented using typescript along with the choice of adding enhanced features (every existing JavaScript code is a valid Typescript code). As typescript code is converted to JavaScript code it makes it easier to integrate into JavaScript projects. It is designed mainly for large scale projects.

TypeScript ===> JavaScript+Type+Some added features

What does TypeScript offer?

  1. Class based objects - Another huge advantage is the use of Classes which provides the ability to use true object oriented programming in our applications and prevents use of prototype based objects. It also provides encapsulation, inheritance and modifiers.
  2. Modularity — It helps make the code more modular.
  3. ES6 Features — Support for ES6 features is also one of the main reasons for its popularity.
  4. Syntax — TypeScript provides syntax which is closer to java and other high level languages (Syntactical Sugaring).

TypeScript Code cannot be interpreted by the Browser directly so there is a need to compile the TypeScript code into plain JavaScript Code, for this purpose we need the TypeScript Compiler (tsc).

TypeScript Compiler (tsc)

  • Written in TypeScript itself.
  • Compiles .ts files to .js files.
  • Installed as an NPM package (NodeJS).
  • Supports ES6 syntax.

TypeScript Vs JavaScript

  • Statically Typed language
  • Supports Modules
  • Provides Errors at Compile time / during development
  • Takes more time as the code needs to be Compiled

2. JavaScript - It is an Object Based Language (Prototype based)

  • Dynamically Typed language
  • Does not Support Modules
  • Doesn’t provide Compile time errors
  • No need of compilation

Why is TypeScript is gaining popularity?

  • The support for Classes and Objects is also one of the main reasons for its increasing popularity as it makes it easier to understand and implement OOPS concepts as compared to the standard prototype based implementation provided by native JavaScript.
  • Also the use of TypeScript in popular JavaScript Frameworks like Angular has helped TypeScript gain interest.

Why do we use TypeScript ?

  1. Code quality — Defining Data Structures in the beginning, using types and interfaces, forces you to think about your app’s data structure from the start and make better design decisions.
  2. Prevents bugs — TypeScript won’t make your software bug free. But it can prevent a lot of type-related errors. Along with the Clever IntelliSense many browsers and IDEs support direct debugging through Source Maps .
  3. Active community — TypeScript is getting more and more popular. It’s used by the top tech companies like Google, Airbnb, Shopify, Asana, Adobe, and Mozilla so we can assume that it reaches their expectations in terms of scalability — as they are developing large and complex applications.
  4. TypeScript Is Just JavaScript — TypeScript starts with JavaScript and ends with JavaScript. Typescript adopts the basic building blocks of your program from JavaScript. All TypeScript code is converted into its JavaScript equivalent for the purpose of execution.

Example:

After Saving the above files we need to transpile the TypeScript Code.

In the terminal, type the following command:

tsc types.js (syntax : tsc filename).

On successful compilation a JavaScript file with the same name and .js extension will be created i.e. types.js containing the transpiled code in the same directory. Now on running the index.html the below output can be seen. As discussed above TypeScript code is transpiled into standard JavaScript Code.

Output:

Conclusion

https://www.buymeacoffee.com/papareact
https://www.buymeacoffee.com/papareact

Thank You!

Darshan mandade
(PAPA Team Writer)

It is an Object Oriented Language (Class based)It is an Object Based Language (Prototype based)Statically Typed languageDynamically Typed languageSupports ModulesDoes not Support ModulesProvides Errors at Compile time / during developmentDoesn’t provide Compile time errorsTakes more time as the code needs to be CompiledNo need of compilation

Why is TypeScript gaining popularity ?

Python Fullstack Developer | Blogger | Web Developer | Mentor. Let's Connect on IG: @Darshu.codes