𝗠𝗮𝘀𝘁𝗲𝗿 𝗧𝘆𝗽𝗲𝗦𝗰𝗿𝗶𝗽𝘁: 𝗣𝗮𝗿𝘁 𝟬𝟭

TypeScript checks your code before it runs. It finds errors while you write. This prevents bugs from reaching your users.

Basic Types TypeScript uses types to define what data looks like.

• String: let message: string = "Hello"; • Number: let age: number = 30; • Boolean: let isActive: boolean = true;

Arrays and Tuples • Arrays: let names: string[] = ["Alice", "Bob"]; • Tuples: let user: [number, string] = [1, "Alice"]; (Order matters here). • Enums: enum Role { Admin, User } (Use named choices instead of random numbers).

Objects and Functions Objects group related data. • let car: { brand: string; year: number } = { brand: "Tesla", year: 2023 }; • Optional fields: Use ? to mark a field as not required.

Functions need types for inputs and outputs. • function add(a: number, b: number): number { return a + b; } • Arrow functions: const multiply = (a: number, b: number): number => a * b;

Advanced Type Tools Type Aliases Create a name for a shape and reuse it. • type User = { id: number; name: string };

Unions and Intersections • Unions (OR): type ID = string | number; • Intersections (AND): type Employee = Person & { salary: number };

Generics Generics use a placeholder to work with many types. • function wrap(value: T): T { return value; }

How to learn:

  1. Type every example.
  2. Break the code on purpose.
  3. Read the errors.
  4. Fix them.

Part 02 will cover advanced type tools.

Source: https://dev.to/mdhemalakhand1999/master-typescript-part-01-452g