DEV Community

Cover image for Introduction to TypeScript
Abhishek Gupta for Coder Studios

Posted on

Introduction to TypeScript

What is TypeScript?

  • TypeScript is a programming language developed by Microsoft that builds on JavaScript by adding static typing.

  • It allows developers to define data types within their code, helping to catch errors during development rather than at runtime. This improves code reliability and enhances developer productivity, as errors can be identified early, making the development process smoother and more predictable.


The Birth of TypeScript

  • JavaScript was originally created for client-side scripting in the mid-90s. However, as websites became more complex over time, developers faced issues like bugs, messy code, and lack of proper error and type checking.

  • To address these challenges, Microsoft created TypeScript in 2012, led by Anders Hejlsberg, to provide a more robust solution for large-scale JavaScript development.


Key Features of TypeScript

1) Static Type Checking

  • TypeScript allows you to check and assign types to variables, parameters, and functions.

  • This helps catch errors early during development and makes your code more readable and maintainable.

let name: string = "Abhishek"; // Correct
name = 123; // Error: Type 'number' is not assignable to type 'string'
Enter fullscreen mode Exit fullscreen mode

2) Type Inference

  • TypeScript can Automatically determine the type of a variable based on it's assigned value

let age = 19; // TypeScript infers 'age' as a number
age = "one"; // Error: Type string is not assignable to type number

Enter fullscreen mode Exit fullscreen mode
  • TypeScript allows you to write less code while still maintaining type safety, making your code more concise and reliable.

3) Object-Oriented Programming (OOP) Support

  • TypeScript provides full support for Object-Oriented Programming (OOP) concepts such as classes, interfaces, inheritance, and more.

4) JavaScript is TypeScript

  • Any valid JavaScript code with a .js extension can be converted to TypeScript simply by changing the file extension from .js to .ts.
  • This makes transitioning to TypeScript easy for developers already familiar with JavaScript.

5) TypeScript Supports JavaScript Libraries

  • All existing JavaScript libraries are valid in TypeScript as well.
  • As a result, developers can seamlessly use and integrate JavaScript libraries, tools, and frameworks within TypeScript projects.

6) Compilation

  • The TypeScript compiler provides built-in error checking. If there is a syntax error in the code, TypeScript will generate a compilation error, allowing you to catch and fix the issue before runtime.

Structure of TypeScript

  • TypeScript code cannot be directly interpreted by the browser, so it must be compiled into plain JavaScript.
  • To accomplish this, we use the TypeScript Compiler (tsc) to convert TypeScript code into JavaScript code that the browser can understand and execute.

Image description

TypeScript Compiler

  • The TypeScript Compiler (tsc) converts TypeScript (.ts) files into JavaScript (.js), making them executable in the browser or Node.js environment.

Steps to Use TypeScript

1) Install TypeScript

  • Open a terminal and run the following command to install TypeScript globally.

  • If you don't have Node.js installed on your system, first install Node.js, which includes npm (Node Package Manager).

npm install --global typescript
Enter fullscreen mode Exit fullscreen mode

2) Verify Installation

  • To verify that TypeScript has been installed successfully, run the following command in your terminal:
tsc --version
Enter fullscreen mode Exit fullscreen mode

3) Compile a TypeScript File

  • After verifying the installation, you can compile your .ts file into JavaScript using the TypeScript compiler.

  • To compile a TypeScript file (example.ts), run the following command in your terminal:

tsc example.ts
Enter fullscreen mode Exit fullscreen mode
  • This will generate an example.js file in the same directory, which can be executed in the browser or Node.js.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

👋 Kindness is contagious

DEV shines when you're signed in, unlocking a customized experience with features like dark mode!

Okay