Typescript

Object

  • Objects in TypeScript are a basic data structure that allows you to store collections of key-value pairs.
  • They can contain various types of data, including primitives, arrays, and functions.
  • Below are the key aspects of objects in TypeScript, explained in bullet points with coding examples.

 

  1. Definition of Objects

    • Key-Value Pairs: An object is an instance that contains a set of key-value pairs, where the keys are strings (or symbols) and the values ​​can be of any data type.
      let person = {
      firstName: "John",
      lastName: "Doe",
      age: 30
      };
  2. Creating Objects:

    • Object Literals: The most common way to create an object is using object literals.
      let car = {
      make: "Toyota",
      model: "Camry",
      year: 2020
      };
    • Using the new Object() Syntax: You can also create an object using the Object constructor.
      let bike = new Object();
      bike.make = "Yamaha";
      bike.model = "MT-07";
  3. Accessing Object Properties:

    • Dot Notation: You can access properties using dot notation.
      console.log(person.firstName); // Outputs: John
    • Bracket Notation: You can also access properties using bracket notation, which is useful for dynamic property names.
      console.log(person["lastName"]); // Outputs: Doe
  4. Modifying Object Properties:

    • You can modify existing properties or add new ones.
      person.age = 31; // Modify existing property
      person.gender = "Male"; // Add new property
  5. Deleting Object Properties:

    • You can remove properties from an object using the `delete` operator.
      delete person.age; // Removes the age property
  6. Methods in Objects:

    • Objects can contain functions as values, which are called methods.
      let calculator = {
      add: function(x: number, y: number): number {
      return x + y;
      }
      };

      console.log(calculator.add(5, 3)); // Outputs: 8
  7. Object Types and Interfaces:

    • Defining Object Types: You can define the structure of an object using interfaces or type aliases.
      • Using Interfaces:
        interface Person {
        firstName: string;
        lastName: string;
        age?: number; // Optional property
        }

        let employee: Person = {
        firstName: "Alice",
        lastName: "Smith"
        };
      • Using Type Aliases:
        type Car = {
        make: string;
        model: string;
        year: number;
        };

        let myCar: Car = {
        make: "Honda",
        model: "Civic",
        year: 2022
        };
  8. Object Types vs Object Type (Uppercase)

    • object Type: Represents all non-primitive values (i.e., anything that is not a primitive type)
      let obj1: object;
      obj1 = { name: "John" }; // Valid

      // obj1 = "Jane"; // Error: Type 'string' is not assignable to type 'object'.
    • Object Type (Uppercase): Refers to the instance of the global Object class and includes methods like `toString()`.
      let obj2: Object;
      obj2 = { name: "Doe" };

      console.log(obj2.toString()); // Outputs something like [object Object]
  9. Empty Object Type ({})

    • The empty type `{}` describes an object that has no properties of its own but can still access methods from the `Object` prototype.
      let emptyObj: {};
      emptyObj = {}; // Valid

      // emptyObj.name = "John"; // Error: Property 'name' does not exist on type '{}'.

Conclusion

  • Objects in TypeScript provide a structured way to organize and manipulate data through key-value pairs.
  • By understanding how to create, access, modify, and define object types using interfaces or type aliases, you can take advantage of the full power of objects in your TypeScript applications.
  • This increases code clarity, maintainability, and type safety, making it easier to work with complex data structures.