Skip to contentPedro Farbo
Lesson 1 / 515 min

What is TypeScript?

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:

typescript
// 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:

typescript
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:

typescript
// 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:

bash
# Global installationnpm install -g typescript # Or as a project dependencynpm install --save-dev typescript

Then, you can compile .ts files:

bash
# Compile a filetsc file.ts # Compile in watch modetsc --watch

Conclusion

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.

Enjoyed the content? Your contribution helps keep everything online and free!

PIX:0737160d-e98f-4a65-8392-5dba70e7ff3e