Skip to content

Latest commit

Β 

History

History
248 lines (187 loc) Β· 5.09 KB

File metadata and controls

248 lines (187 loc) Β· 5.09 KB

Back to Typescript Home Page

βœ… Basic Topics βœ¨πŸ“˜πŸ§©

These topics cover the core syntax and features TypeScript adds to JavaScript:


What is TypeScript and Why Use It

TypeScript is a superset of JavaScript that adds static typing and other features to improve code quality and productivity.

πŸ”Ή Why Use TypeScript?

  • ✨ Error Prevention: Catches bugs during development.
  • πŸ“– Improved Readability: Explicit types make code easier to understand.
  • πŸš€ Modern Features: Supports ES6+ features and compiles to older JavaScript.
function add(a: number, b: number): number {
  return a + b;
}

add(5, "10"); // ❌ Error: Argument of type 'string' is not assignable to parameter of type 'number'.

Back to Top


Installing & Setting Up TypeScript

πŸ”Ή Installation:

  • πŸ› οΈ Install TypeScript globally:
    npm install -g typescript

πŸ”Ή Setting Up a Project:

  1. πŸ“‚ Initialize a project:
    npm init -y
  2. πŸ“¦ Install TypeScript locally:
    npm install --save-dev typescript
  3. πŸ“ Create a tsconfig.json file:
    npx tsc --init

πŸ”Ή Compiling TypeScript:

  • πŸ”„ Compile .ts files to .js:
    npx tsc index.ts

Back to Top


TypeScript Compiler (tsc) & Configuration (tsconfig.json)

The TypeScript compiler (tsc) converts TypeScript into JavaScript. The tsconfig.json file configures the compiler.

πŸ”Ή Example tsconfig.json:

{
  "compilerOptions": {
    "target": "ES6", // πŸ› οΈ Specify JavaScript version
    "strict": true, // πŸ”’ Enable strict type checking
    "outDir": "./dist" // πŸ“‚ Output directory
  },
  "include": ["src/**/*"], // πŸ“‹ Files to include
  "exclude": ["node_modules"] // 🚫 Files to exclude
}

Back to Top


Basic Types

πŸ”Ή Common Types:

  • πŸ”’ number, πŸ“ string, βœ… boolean:
    let age: number = 25;
    let name: string = "Alice";
    let isStudent: boolean = true;

πŸ”Ή Special Types:

  • 🌐 any: Allows any type (use sparingly).
    let value: any = 42;
    value = "Hello";
  • πŸ”’ unknown: Safer alternative to any.
    let value: unknown = "Hello";
    if (typeof value === "string") console.log(value.toUpperCase());
  • 🚫 void: For functions that don’t return a value.
    function logMessage(message: string): void {
      console.log(message);
    }
  • ❌ never: For functions that never return.
    function throwError(message: string): never {
      throw new Error(message);
    }

Back to Top


Type Annotations vs Type Inference

  • πŸ–ŠοΈ Type Annotations: Explicitly specify types.
    let age: number = 25;
  • πŸ” Type Inference: TypeScript infers the type.
    let age = 25; // Inferred as number

Back to Top


Arrays and Tuples

  • πŸ“‹ Arrays: Store multiple values of the same type.
    let numbers: number[] = [1, 2, 3];
  • πŸ”— Tuples: Fixed-length arrays with specific types.
    let person: [string, number] = ["Alice", 25];

Back to Top


Enums

Enums define a set of named constants.

enum Direction {
  Up,
  Down,
  Left,
  Right,
}

let move: Direction = Direction.Up;
console.log(move); // 0

Back to Top


Type Aliases

Type aliases create custom types.

type Point = {
  x: number;
  y: number;
};

let point: Point = { x: 10, y: 20 };

Back to Top


Union and Intersection Types

  • πŸ”€ Union Types: A value can be one of several types.
    let value: string | number = "Hello";
    value = 42;
  • πŸ”— Intersection Types: Combine multiple types.
    type A = { a: number };
    type B = { b: string };
    type C = A & B;
    
    let obj: C = { a: 1, b: "Hello" };

Back to Top


Literal Types

Literal types allow specific values as types.

let direction: "up" | "down" = "up";

Back to Top


const vs let with Types

  • πŸ”’ Use const for variables that won’t change:
    const name: string = "Alice";
  • πŸ”„ Use let for variables that may change:
    let age: number = 25;
    age = 26;

Back to Top