Lesson 5 of 60 – Installing TypeScript
8%

Installing TypeScript

Before writing and compiling TypeScript programs, we need to set up a TypeScript development environment. The most common way to install TypeScript is by using Node.js and the npm package manager.

Note: TypeScript can be installed globally for general command-line use or locally inside a project. For real projects, local installation is usually preferred because the project can use its own TypeScript version.

1. What Do We Need to Install TypeScript?

To install TypeScript using npm, you normally need:

  • Node.js
  • npm
  • TypeScript
  • A code editor such as Visual Studio Code

Node.js provides the JavaScript runtime, while npm is used to install JavaScript and TypeScript packages.

2. What is Node.js?

Node.js is a JavaScript runtime that allows JavaScript programs to run outside a web browser.

It is commonly used for development tools, servers, command-line applications, and JavaScript-based projects.

3. What is npm?

npm is the package manager commonly used with Node.js projects. It allows developers to install, update, and manage packages.

TypeScript can be installed using npm.

npm install typescript

4. Check Whether Node.js is Installed

Open Command Prompt, PowerShell, or a terminal and run:

node --version

If Node.js is installed, the command displays the installed Node.js version.

5. Check the npm Version

You can check whether npm is available by using:

npm --version

If npm is available, the terminal will display its version number.

6. Install Node.js First

If the node and npm commands are not available, install Node.js first.

After installation, close and reopen your terminal and check the versions again.

node --version
npm --version

7. Create a TypeScript Project Folder

Create a folder for your TypeScript project.

mkdir typescript-demo
cd typescript-demo

The cd command moves the terminal into the project folder.

8. Initialize an npm Project

Inside the project folder, initialize npm using:

npm init -y

This creates a package.json file.

9. What is package.json?

The package.json file contains information about an npm project and its dependencies.

It can contain:

  • Project name
  • Version
  • Scripts
  • Dependencies
  • Development dependencies

10. Install TypeScript Locally

For a project, TypeScript can be installed locally as a development dependency.

npm install --save-dev typescript

This adds TypeScript to the project's development dependencies.

11. Why Install TypeScript Locally?

A local installation allows a project to use a specific TypeScript version rather than depending on one global installation.

This makes project environments more predictable.

12. Check the Local TypeScript Installation

After installing TypeScript locally, you can check it using:

npx tsc --version

The npx command can run the TypeScript compiler installed in the current project.

13. What is tsc?

tsc is the TypeScript compiler command. It is used to compile TypeScript files into JavaScript.

tsc app.ts

The compiler reads the TypeScript file and produces JavaScript output according to the project configuration.

14. Run tsc with npx

When TypeScript is installed locally, you can run the compiler with:

npx tsc

This is useful because it uses the TypeScript compiler available within the project.

15. Global TypeScript Installation

TypeScript can also be installed globally using npm.

npm install -g typescript

A global installation makes the tsc command available from different project directories.

16. Local vs Global Installation

Feature Local Global
Installation Inside project System-wide
Command npx tsc tsc
Project-specific version Yes Not project-specific
Useful for projects Yes General command-line use

17. Create Your First TypeScript File

Create a file named app.ts.

let message: string = "Hello TypeScript";

console.log(message);

The .ts extension identifies the file as a TypeScript file.

18. Compile app.ts

Use the following command to compile the TypeScript file:

npx tsc app.ts

The compiler generates a JavaScript file from the TypeScript source.

19. The Generated JavaScript File

After compilation, a JavaScript file can be generated. For a simple file named app.ts, the output can be app.js.

app.ts
   ↓
TypeScript Compiler
   ↓
app.js

20. Run the JavaScript File

If the generated JavaScript file is intended to run in Node.js, you can execute it with:

node app.js

The program will execute the generated JavaScript.

21. Install TypeScript with a Specific Project

A common project setup is:

mkdir my-typescript-app
cd my-typescript-app

npm init -y

npm install --save-dev typescript

This creates a project and installs TypeScript locally.

22. Create tsconfig.json

A TypeScript project can use a configuration file named tsconfig.json.

The file tells the TypeScript compiler how the project should be compiled.

You can create one using:

npx tsc --init

23. What is tsconfig.json?

The tsconfig.json file contains TypeScript compiler configuration options.

It can control settings such as:

  • Target JavaScript version
  • Module system
  • Source directories
  • Output directories
  • Strict type checking

24. Compile a Project

When a project contains a tsconfig.json file, you can compile the project using:

npx tsc

The compiler reads the configuration file and compiles the project according to its settings.

25. TypeScript in Visual Studio Code

Visual Studio Code provides strong TypeScript development support.

It can provide:

  • Syntax highlighting
  • Autocomplete
  • Error highlighting
  • Code navigation
  • Type information

This makes it a popular editor for TypeScript development.

26. Installing TypeScript for React Projects

React projects can use TypeScript for typed components and application logic.

When creating a new React project, the project setup can include TypeScript support. Existing projects can also add TypeScript and appropriate type definitions.

Important: The exact setup depends on the React project tool and project structure.

27. TypeScript for React Native

React Native applications can also be written using TypeScript. Modern React Native project templates can be configured for TypeScript.

TypeScript files containing JSX use the .tsx extension.

App.tsx
HomeScreen.tsx
ProfileScreen.tsx

28. Basic Installation Workflow

A basic TypeScript project workflow is:

  1. Install Node.js.
  2. Verify Node.js and npm.
  3. Create a project folder.
  4. Initialize npm.
  5. Install TypeScript.
  6. Create a TypeScript file.
  7. Create a TypeScript configuration file.
  8. Compile the project.
Node.js
   ↓
npm
   ↓
TypeScript
   ↓
.ts files
   ↓
tsc
   ↓
JavaScript

29. Complete Basic Installation Example

mkdir typescript-demo

cd typescript-demo

npm init -y

npm install --save-dev typescript

npx tsc --init

Then create app.ts:

let message: string = "Hello TypeScript";

console.log(message);

Compile the project:

npx tsc

30. Installation Checklist

Before starting TypeScript development, make sure you understand the following setup steps:

  • Node.js is installed.
  • npm is available.
  • A project folder is created.
  • package.json is created.
  • TypeScript is installed.
  • tsc is available.
  • A .ts file can be created.
  • tsconfig.json can be created.
  • The TypeScript project can be compiled.
Remember: For a project-based workflow, installing TypeScript locally with npm and using npx tsc keeps the TypeScript compiler associated with that project.

📌 Key Points

  • Node.js provides the environment commonly used with npm-based TypeScript projects.
  • npm is used to install TypeScript and other packages.
  • TypeScript can be installed locally or globally.
  • Local installation is useful for project-specific versions.
  • tsc is the TypeScript compiler command.
  • npx tsc can run a locally installed TypeScript compiler.
  • npm init -y creates a basic package.json file.
  • npm install --save-dev typescript installs TypeScript as a development dependency.
  • npx tsc --init creates a TypeScript configuration file.
  • React Native projects can use TypeScript and .tsx files.

🧠 Quick Quiz

Question: Which command installs TypeScript locally as a development dependency?