TypeScript > JavaScript
menu-icon close-icon

Published on : Jan 29,2023

Author : Bijoy Kar

TypeScript > JavaScript

Hi, everyone I am Bijoy Kar. Thank you for reading TypeScript > JavaScript. Thank you to all of you for supporting me. I am quite active, I post daily on Twitter and LinkedIn so if you like then give me a follow.


Introduction

TypeScript is a programming language that is a super set of JavaScript. It was developed and is maintained by Microsoft. TypeScript adds optional static typing, class-based object-oriented programming, and other features to JavaScript. These features make it easier to write, maintain, and scale large applications.

One of the biggest advantages of TypeScript is its static typing. This means that variables, function parameters, and return types can be explicitly defined. This can help catch errors early on in the development process, before the code is even run. This can save a lot of time and effort in debugging and testing.

TypeScript also adds support for classes and interfaces, which are fundamental concepts in object-oriented programming. Classes allow for the creation of objects with specific properties and methods, and interfaces allow for the creation of a contract for objects to adhere to. These features make it easier to structure and organize code, making it more readable and maintainable.

Another advantage of TypeScript is its compatibility with existing JavaScript code. TypeScript code can be transpiled, or converted, into JavaScript, which can then be run on any platform that supports JavaScript. This means that developers can start using TypeScript in their existing JavaScript projects without having to completely rewrite the code.

TypeScript also has a robust and active community, with many open-source libraries and frameworks available. This includes popular libraries such as React, Angular, and Vue.js, which have official TypeScript support. This means that developers can use TypeScript with these libraries and frameworks, taking advantage of its features while still using the tools they are familiar with.

Overall, TypeScript is a powerful programming language that can make development easier, more efficient, and more scalable. Its static typing and object-oriented programming features can help catch errors early on and make code more readable and maintainable. Its compatibility with existing JavaScript code and active community make it a great choice for developers.

Advantages

  • Static Typing: TypeScript adds optional static typing, which means that variables, function parameters, and return types can be explicitly defined. This can help catch errors early on in the development process, before the code is even run, saving time and effort in debugging and testing.
  • Object-Oriented Programming: TypeScript adds support for classes and interfaces, which are fundamental concepts in object-oriented programming. Classes allow for the creation of objects with specific properties and methods, and interfaces allow for the creation of a contract for objects to adhere to. This makes it easier to structure and organize code, making it more readable and maintainable.
  • Improved IntelliSense: TypeScript includes improved IntelliSense, an auto-completion and code-suggestion feature, which makes coding easier and more efficient. It also provides a better development experience with better error reporting and debugging.
  • Compatibility: TypeScript code can be transpiled, or converted, into JavaScript, which can then be run on any platform that supports JavaScript. This means that developers can start using TypeScript in their existing JavaScript projects without having to completely rewrite the code.
  • Large and Active Community: TypeScript has a robust and active community, with many open-source libraries and frameworks available. This includes popular libraries such as React, Angular, and Vue.js, which have official TypeScript support. This means that developers can use TypeScript with these libraries and frameworks, taking advantage of its features while still using the tools they are familiar with.
  • Better scalability: With TypeScript, codebase can be more scalable, maintainable and robust, especially in large projects, as it enforces a stricter code structure, and provides better error reporting.

Getting Started with TypeScript

Prerequisites

  • Nodejs must be installed in your local system
  • Must know basics of JavaScript

Getting Started

  • Install TypeScript: First, you will need to install TypeScript on your computer. You can do this by running the command npm install -g typescript if you have Node.js and npm (Node Package Manager) installed. If you are using yarn, you can run the command yarn global add typescript.

npm install :

npm install -g typescript

yarn install :

yarn global add typescript
  • Create a TypeScript file: Next, you will need to create a new file with a .ts extension. You can use any text editor to create this file. For example, you can create a file called index.ts.

  • Write some TypeScript code: In your new file, you can start writing TypeScript code. You can start by writing a simple function or variable and add types to it. For example :

function getSum(first: number, second: number)
    return first + second;
}
console.log(getSum(10, 11));
console.log(getSum(13, 31));
console.log(getSum(10.44, 115.11));
  • Compile the TypeScript code: To run the TypeScript code, you need to first compile it into JavaScript. You can do this by running the command tsc index.ts in your terminal. This will create a new file called index.js in the same directory as your TypeScript file.
tsc index.ts
  • Run the JavaScript code: Finally, you can run the generated JavaScript code by running the command node index.js in your terminal. This will execute your TypeScript code.
node index.js
  • Use a TypeScript-enabled editor: To improve development experience you can use a code editor that supports TypeScript, such as Visual Studio Code, WebStorm, or Atom. They provide features like code completion, error highlighting, and debugging which are helpful when working with TypeScript.

Thank you for reading this article, if like to read more of my article then subscribe my newsletter Weekly Coding Dose and give a follow. I post daily on LinkedIn and Twitter.

Author

Bijoy Kar

Full Stack Developer | Student