Object Type in TypeScript

  • In TypeScript, the Object type represents any non-primitive value, i.e., it can be used to describe any value that is not of a primitive type (such as number, string, boolean, null, or undefined). The Object type can be used to define variables, parameters, or return types that can hold or reference any kind of object.
Here's an example that demonstrates the usage of the Object type in TypeScript:
  • Example 1:


    const person: { name: string, age: number } = {
        name: 'John',
        age: 30,
    };

    console.log(person);
    console.log(person.name);
    console.log(person.age);

{ name: 'John', age: 30 }
John
30
  • Example 2:

    type personType = { name: string, age: number }

    const person: personType = {
        name: 'John',
        age: 30,
    };

    console.log(person);
    console.log(person.name);
    console.log(person.age);


{ name: 'John', age: 30 }
John
30

No comments:

Post a Comment

Debouncing and Throttling in JavaScript

Debouncing and Throttling - Made Simple! Think of these as traffic controllers for your functions: Debouncing = Wait until user stops, ...