- TypeScript Tutorial
- TypeScript Home
- TypeScript Introduction
- TypeScript Setup
- TypeScript First Program
- TypeScript vs JavaScript
- TypeScript Data Types
- TypeScript Type Inference
- TypeScript Type Annotations
- TypeScript Interfaces
- TypeScript Enums
- TypeScript Type Aliases
- TypeScript Type Assertions
- TypeScript Variables
- TypeScript Functions
- TypeScript Functions
- TypeScript Optional Parameters
- TypeScript Default Parameters
- TypeScript Rest Parameters
- TypeScript Arrow Functions
- Classes and Objects
- Introduction to Classes
- Properties and Methods
- Access Modifiers
- Static Members
- Inheritance
- Abstract Classes
- Interfaces vs Classes
- Advanced Types
- TypeScript Union Types
- TypeScript Intersection Types
- TypeScript Literal Types
- TypeScript Nullable Types
- TypeScript Type Guards
- TypeScript Discriminated Unions
- TypeScript Index Signatures
- TypeScript Generics
- Introduction to Generics
- TypeScript Generic Functions
- TypeScript Generic Classes
- TypeScript Generic Constraints
- TypeScript Modules
- Introduction to Modules
- TypeScript Import and Export
- TypeScript Default Exports
- TypeScript Namespace
- Decorators
- Introduction to Decorators
- TypeScript Class Decorators
- TypeScript Method Decorators
- TypeScript Property Decorators
- TypeScript Parameter Decorators
- Configuration
- TypeScript tsconfig.json File
- TypeScript Compiler Options
- TypeScript Strict Mode
- TypeScript Watch Mode
TypeScript Setup
TypeScript is a "typed" superset of JavaScript that catches errors during development rather than at runtime. However, browsers and Node.js cannot run TypeScript files directly. This guide will walk you through setting up a professional environment where your TypeScript code is efficiently compiled into clean JavaScript.
Prerequisites
- Node.js Installed: You need Node.js to manage packages and run the TypeScript compiler. You can check your version by running
node -vin your terminal. - Code Editor: Visual Studio Code (VS Code) is the industry standard for TypeScript because it includes built-in support for IntelliSense and real-time error checking.
Steps to Install TypeScript
1. Install TypeScript via npm
To use the TypeScript compiler (tsc) anywhere on your system, install it globally using the Node Package Manager (npm):
npm install -g typescript
Verify that the compiler is ready by checking the version:
tsc --version
sudo npm install -g typescript if you encounter permission errors. Alternatively, for specific projects, it is often better to install TypeScript locally using npm install --save-dev typescript.
2. Initialize a TypeScript Project
Instead of manually configuring your settings, you can generate a tsconfig.json file. This file acts as the "brain" of your project, telling the compiler how to behave.
tsc --init
This command creates a configuration file with sensible defaults. Inside, you can specify things like which version of JavaScript to target (e.g., ES6) or where your compiled files should go.
tsconfig.json and set the "outDir": "./dist" and "rootDir": "./src" properties. This keeps your project organized by separating your source TypeScript files from the generated JavaScript output.
3. Write Your First TypeScript File
Create a file named app.ts. In this example, we’ll use a "Type Annotation" for the function parameter to ensure only strings are passed in.
const greet = (name: string): string => {
return `Hello, ${name}! Your TypeScript setup is working.`;
};
console.log(greet("Developer"));
node app.ts directly. Node.js understands JavaScript, not TypeScript. If you try this, you will get a SyntaxError because Node won't recognize the : string type annotations.
4. Compile TypeScript to JavaScript
Now, use the compiler to transform your TypeScript into browser-ready JavaScript:
tsc app.ts
After running this, you will notice a new file named app.js in your folder. The compiler has stripped away the type annotations, leaving valid JavaScript.
tsc -w (Watch Mode). This command tells TypeScript to watch your files for changes. Every time you hit "Save," it will automatically recompile your code so you don't have to run the command manually.
5. Run the Compiled JavaScript File
Finally, execute your code using the Node.js runtime:
node app.js
Summary
Setting up TypeScript is a foundational skill for modern web development. By installing the compiler via npm, initializing a tsconfig.json for project rules, and using the tsc command, you create a robust workflow. This setup allows you to catch bugs early through static typing while still delivering standard JavaScript to your users.