DEV Community

Robertino
Robertino

Posted on

πŸ’» What is TypeScript? A Practical Guide for Developers πŸ› 

What is TypeScript

TypeScript is a popular JavaScript superset created by Microsoft that brings a type system on top of all the flexibility and dynamic programming capabilities of JavaScript.

The language has been built as an open-source project, licensed under the Apache License 2.0, has a very active and vibrant community, and has taken off significantly since its original inception.

Installing TypeScript

To get started with TypeScript and try out all the examples, you can either install the TypeScript transpiler on your computer (more about this in the following paragraph), use the official online playground or any other online solution you prefer.

In case you want to try the examples locally, you need to install the command-line transpiler, which runs on Node. First, you need to install Node.js and npm on your system. Then, you can create a Node.js project and install the TypeScript transpiler package:

# Create a new directory for your project
mkdir typescript-intro

# Make your project directory the current directory
cd typescript-intro

# Initialize a new Node.js project
npm init -y

# Install the TypeScript compiler
npm i typescript
Enter fullscreen mode Exit fullscreen mode

This will install the tsc (TypeScript Compiler) command in the current project. To test the installation, create a TypeScript file called index.ts under your project directory with the following code:

console.log(1);
Enter fullscreen mode Exit fullscreen mode

Then, use the transpiler to transform it to JavaScript:

# transpiling index.ts to index.js
npx tsc index.ts
Enter fullscreen mode Exit fullscreen mode

This will generate a new file called index.js with the exact same code of the TypeScript file. Use the node command to execute this new file:

# this will output 1
node index.js
Enter fullscreen mode Exit fullscreen mode

Although the transpiler did nothing else besides creating a JavaScript file and copying the original code to it, these steps helped you validate that your TypeScript installation is in good shape and ready to handle the next steps.

Note: TypeScript versions can have substantial differences even though they get released as minor revisions. It's common to bump into transpilation problems after a minor version update. For that reason, it is better to install TypeScript locally in your project and execute it using npx when needed instead of relying on a global TypeScript installation.

Read more...

Top comments (0)