Getting Started with TypeScript Setup and Installation
Learn how to set up and install TypeScript for your first project, including fixing common errors.
TypeScript is a powerful programming language that builds on JavaScript by adding static types. This helps catch errors earlier and makes coding more efficient. In this tutorial, you'll learn how to install and set up TypeScript on your computer and create your very first TypeScript file.
First, you need to have Node.js installed, as TypeScript uses npm (Node Package Manager) to install packages. After confirming Node.js is installed, you can install the TypeScript compiler globally by running 'npm install -g typescript'. This allows you to use the 'tsc' command to compile TypeScript files into JavaScript.
npm install -g typescript
ts --version
// This command checks if TypeScript was installed correctly by printing the version number
// Create a file named 'hello.ts' with the following content:
const message: string = 'Hello, TypeScript!';
console.log(message);Once you have the 'hello.ts' file, compile it by running 'tsc hello.ts' in your terminal. This generates a 'hello.js' file you can run with Node.js using 'node hello.js'. If you encounter errors like 'tsc: command not found', it means TypeScript was either not installed correctly or your system's PATH variable does not include the npm global packages folder. Reinstall TypeScript or add the npm global directory to your PATH to fix this.