What is TypeScript?
TypeScript is a superset of JavaScript developed by Microsoft that adds optional static typing to the language. This means that all valid JavaScript code is also valid TypeScript code.
Why Use TypeScript?
1. Error Detection at Development Time
With TypeScript, many common errors are caught before even running the code:
// JavaScript - error only appears at runtimefunction add(a, b) { return a + b;}add("5", 3); // "53" - concatenation instead of addition! // TypeScript - error appears immediatelyfunction add(a: number, b: number): number { return a + b;}add("5", 3); // Error: Argument of type 'string' is not assignable to parameter of type 'number'2. Better Autocomplete and IntelliSense
With defined types, your IDE knows exactly which methods and properties are available:
interface User { name: string; email: string; age: number;} const user: User = { name: "Pedro", email: "pedro@email.com", age: 30}; // Your IDE suggests: name, email, ageuser. // ← autocomplete works perfectly!3. Safe Refactoring
When you rename a variable or change an object's structure, TypeScript shows all the places that need to be updated.
4. Integrated Documentation
Types serve as documentation that never gets outdated:
// The function signature already documents what it expects and returnsfunction calculateDiscount( price: number, percentage: number): number { return price * (1 - percentage / 100);}How Does TypeScript Work?
TypeScript doesn't run directly in the browser or Node.js. It needs to be transpiled (converted) to JavaScript:
file.ts → TypeScript Compiler (tsc) → file.js
The resulting code is pure JavaScript that can run in any environment.
Installing TypeScript
To start using TypeScript, you need to install it globally or as a project dependency:
# Global installationnpm install -g typescript # Or as a project dependencynpm install --save-dev typescriptThen, you can compile .ts files:
# Compile a filetsc file.ts # Compile in watch modetsc --watchConclusion
TypeScript is a powerful tool that brings safety and productivity to JavaScript projects. In the next lesson, we'll explore the basic types available in the language.