RunJS LogoRunJS

How to Run TypeScript: A Step-by-Step Guide

Published on

run TypeScript

TypeScript has become an important part of modern web development, offering powerful features like static typing, classes, and interfaces to enhance JavaScript applications. If you're looking to improve your coding skills and write more robust code, learning how to run TypeScript is a great place to start. This guide will walk you through various methods to run TypeScript, from setting up a local development environment to using online tools and build systems.

What is TypeScript?

TypeScript is an open-source programming language developed by Microsoft. It is a typed superset of JavaScript that compiles to plain JavaScript. TypeScript adds optional static typing, classes, and interfaces, making it ideal for large-scale applications and improving developer productivity.

Why Use TypeScript?

  • Static Typing: Catch errors during development rather than at runtime.
  • Enhanced Tooling: Improved code completion, refactoring, and navigation support.
  • Better Maintainability: Easier to manage large codebases with clear interfaces and types.
  • Modern JavaScript Features: Access to the latest ECMAScript features before they are widely supported.

Running TypeScript Locally

Running TypeScript locally involves setting up your environment to compile TypeScript code into JavaScript, which can then be executed. Here's how to do it:

Step 1: Install Node.js and npm

First, download and install Node.js if you haven't already. Node.js comes with npm, the Node Package Manager.

Verify Installation:

node -v
npm -v

Step 2: Install the TypeScript Compiler

Install the TypeScript compiler globally using npm:

npm install -g typescript

Step 3: Write TypeScript Code

Create a new directory for your project and navigate into it:

mkdir typescript-demo
cd typescript-demo

Create a file named app.ts:

// app.ts
function greet(name: string): string {
return `Hello, ${name}! Welcome to TypeScript.`;
}
console.log(greet("Developer"));

Step 4: Compile TypeScript to JavaScript

Compile your TypeScript code using the tsc command:

tsc app.ts

This generates an app.js file in the same directory.

Step 5: Run the JavaScript Code

Execute the compiled JavaScript file with Node.js:

node app.js

Output:

Hello, Developer! Welcome to TypeScript.

Using ts-node for Seamless Execution

ts-node allows you to run TypeScript code directly without manual compilation.

Step 1: Install ts-node

Install ts-node globally:

npm install -g ts-node

Step 2: Run TypeScript Code Directly

Run your TypeScript file:

ts-node app.ts

Output:

Hello, Developer! Welcome to TypeScript.

Setting Up TypeScript in Visual Studio Code

Visual Studio Code is a free, powerful code editor with excellent TypeScript support.

Step 1: Install Visual Studio Code

Download and install VS Code from the official website.

Step 2: Open Your Project

Open your typescript-demo folder in VS Code.

Step 3: Initialize TypeScript Configuration

In the terminal, run:

tsc --init

This creates a tsconfig.json file, which TypeScript uses to manage project settings.

Step 4: Configure tsconfig.json

Edit tsconfig.json to set your compiler options:

{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./dist",
"strict": true
},
"include": ["./**/*.ts"],
"exclude": ["node_modules"]
}

Step 5: Compile and Run

Compile your project:

tsc

Run the compiled code:

node dist/app.js

Running TypeScript with Build Tools

For larger projects, integrating TypeScript with build tools like Webpack or Babel can streamline your workflow.

Using Webpack

Step 1: Install Dependencies

npm install --save-dev webpack webpack-cli ts-loader typescript

Step 2: Configure Webpack

Create a webpack.config.js file:

// webpack.config.js
const path = require("path");
module.exports = {
entry: "./app.ts",
module: {
rules: [
{ test: /\.ts$/, use: "ts-loader", exclude: /node_modules/ },
],
},
resolve: {
extensions: [".ts", ".js"],
},
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist"),
},
};

Step 3: Update tsconfig.json

Ensure your tsconfig.json aligns with Webpack:

{
"compilerOptions": {
"target": "es6",
"module": "esnext",
"strict": true,
"esModuleInterop": true
}
}

Step 4: Build and Run

Build your project:

npx webpack

Run the bundle:

node dist/bundle.js

Using Babel

Step 1: Install Dependencies

npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/preset-typescript

Step 2: Create Babel Configuration

Create a .babelrc file:

{
"presets": ["@babel/preset-env", "@babel/preset-typescript"]
}

Step 3: Compile TypeScript with Babel

npx babel app.ts --out-file dist/app.js

Step 4: Run the Compiled Code

node dist/app.js

Online TypeScript Playgrounds

If you prefer not to set up a local environment, you can use online tools to write and run TypeScript code.

TypeScript Playground

The TypeScript Playground is an official online editor that lets you write TypeScript code and see the compiled JavaScript output instantly.

Features:

  • Real-time compilation
  • Option to share code via URL
  • Configurable compiler options

Quick Prototyping with RunJS

RunJS is a desktop playground for JavaScript and TypeScript.

RunJS TypeScript example

Features of RunJS

  • Immediate Feedback: See the results of your code as you type.
  • TypeScript Support: Write TypeScript code without any setup.
  • NPM Packages: Import and use npm packages directly.

Getting Started with RunJS

  1. Download and Install: Get RunJS from the homepage.
  2. Write TypeScript Code: Open RunJS and start coding in TypeScript. The app handles compilation behind the scenes.
  3. No Configuration Needed: RunJS requires no setup, making it ideal for testing snippets and learning TypeScript.

Tips for Running TypeScript Effectively

  • Use a Linter: Tools like ESLint can help maintain code quality.
  • Type Definitions: Leverage DefinitelyTyped for third-party library type definitions.
  • Incremental Compilation: Use tsc --watch for automatic recompilation on file changes.
  • Debugging: Utilize source maps for debugging TypeScript code in editors and browsers.

Conclusion

Running TypeScript is straightforward once you understand the various options available. Whether you prefer setting up a local development environment, using online tools, or leveraging applications like RunJS for quick prototyping, TypeScript offers flexibility to suit your workflow. Embrace TypeScript to write safer, more maintainable code and enhance your development experience.

TwitterShareFacebookShareLinkedInShareRedditShareY CombinatorShare

Join the RunJS mailing list

You'll receive an occasional newsletter with development updates and articles from the blog. Unsubscribe at any time.

Recent posts