Skip to main content

TypeScript

What is TypeScript?

TypeScript is an open-source programming language developed and maintained by Microsoft. It is a statically typed superset of JavaScript, which means it builds on JavaScript by adding static types. This allows developers to catch errors early during development, enhance code readability, and improve maintainability.

Installing TypeScript

Please follow the instructions at this link to install and set up TypeScript and the dependencies you need to have installed to make it work.

Key Features of TypeScript

  1. Static Typing:
    • TypeScript introduces static typing to JavaScript, allowing developers to define types for variables, function parameters, and return values.

    • Example:

      typescriptCopy code
      let age: number = 25;
      function greet(name: string): string {
      return `Hello, ${name}!`;
      }
  2. Enhanced IDE Support:
    • TypeScript provides better tooling and autocompletion in editors like Visual Studio Code.
    • Features such as IntelliSense, real-time type checking, and error reporting enhance the development experience.
  3. Type Inference:
    • TypeScript can automatically infer types based on the assigned values, reducing the need for explicit type annotations.

    • Example:

      typescriptCopy code
      let username = 'John'; // inferred as string

  4. Interfaces and Type Aliases:
    • Interfaces define the shape of objects, while type aliases can define custom types.

    • Example:

      typescriptCopy code
      interface User {
      name: string;
      age: number;
      }

      type ID = string | number;

  5. Classes and Object-Oriented Programming:
    • TypeScript supports classes, inheritance, and other OOP features.

    • Example:

      typescriptCopy code
      class Person {
      private name: string;
      constructor(name: string) {
      this.name = name;
      }
      greet() {
      return `Hello, my name is ${this.name}`;
      }
      }
  6. Modules and Namespaces:
    • TypeScript uses ES6 modules to organize code into reusable components.

    • Example:

      typescriptCopy code
      // math.ts
      export function add(a: number, b: number): number {
      return a + b;
      }

      // main.ts
      import { add } from './math';
      console.log(add(2, 3)); // Output: 5

  7. Generics:
    • Generics allow the creation of reusable components that work with any data type.

    • Example:

      typescriptCopy code
      function identity<T>(arg: T): T {
      return arg;
      }
      let output = identity<string>('Hello');

  8. Advanced Types:
    • TypeScript provides advanced types like union, intersection, and conditional types for more complex type definitions.

    • Example:

      typescriptCopy code
      type Shape = Circle | Square;

Why Use TypeScript?

  • Error Detection: Catch errors at compile time rather than runtime.
  • Improved Readability: Clear type annotations make the code easier to understand and maintain.
  • Refactoring: Enhanced tooling support for refactoring code safely.
  • Compatibility: TypeScript code transpiles to JavaScript, ensuring compatibility with all JavaScript environments.

Setting Up TypeScript

  1. Installation:
    • Install TypeScript globally using npm:

      shCopy code
      npm install -g typescript

  2. Creating a TypeScript Project:
    • Initialize a new TypeScript project:

      shCopy code
      tsc --init

    • This generates a tsconfig.json file to configure the TypeScript compiler options.

  3. Compiling TypeScript Code:
    • Write TypeScript code in .ts files.

    • Compile the code to JavaScript:

      shCopy code
      tsc

Basic TypeScript Syntax

  1. Variables:

    typescriptCopy code
    let isDone: boolean = false;
    let decimal: number = 6;
    let color: string = 'blue';

  2. Functions:

    typescriptCopy code
    function add(x: number, y: number): number {
    return x + y;
    }

  3. Arrays:

    typescriptCopy code
    let list: number[] = [1, 2, 3];
  4. Tuples:

    typescriptCopy code
    let tuple: [string, number];
    tuple = ['hello', 10];
  5. Enums:

    typescriptCopy code
    enum Color {
    Red,
    Green,
    Blue
    }
    let c: Color = Color.Green;

Advanced TypeScript Concepts

  1. Interfaces:

    typescriptCopy code
    interface Person {
    firstName: string;
    lastName: string;
    }

    function greet(person: Person) {
    return `Hello, ${person.firstName} ${person.lastName}`;
    }
  2. Generics:

    typescriptCopy code
    function identity<T>(arg: T): T {
    return arg;
    }

    let output = identity<number>(42);
  3. Modules:

    typescriptCopy code
    // math.ts
    export function add(a: number, b: number): number {
    return a + b;
    }

    // app.ts
    import { add } from './math';
    console.log(add(5, 3)); // Output: 8

Conclusion

TypeScript enhances JavaScript by adding type safety, better tooling, and modern language features. By learning TypeScript, you can write more reliable and maintainable code, which is particularly beneficial for large-scale applications. Start exploring TypeScript today to elevate your JavaScript development skills!