How to use Object in TypeScript

1 Answer

0 votes
/**
 * Object Example in TypeScript
 * ----------------------------
 * Demonstrates:
 * - Creating typed objects
 * - Accessing properties
 * - Updating properties
 * - Adding methods
 * - Iterating over objects
 * - Using Object utility methods
 * - Creating reusable functions
 */

/**
 * Define the shape of a Person object.
 */
interface Person {
    firstName: string;
    lastName: string;
    age: number;
    isEmployed: boolean;
    city?: string; // Optional property
}

/**
 * Returns a formatted description of a person.
 *
 * @param person Person object
 * @returns Formatted string
 */
function getPersonSummary(person: Person): string {
    return `${person.firstName} ${person.lastName} is ${person.age} years old.`;
}

/**
 * Returns a new Person object with an updated age.
 * Does not modify the original object.
 *
 * @param person Original person
 * @param newAge Updated age
 * @returns New person object
 */
function updateAge(person: Person, newAge: number): Person {
    return {
        ...person,
        age: newAge
    };
}

// ------------------------------------------------------------------
// Create an object
// ------------------------------------------------------------------

const person: Person = {
    firstName: "John",
    lastName: "Doe",
    age: 30,
    isEmployed: true
};

console.log("=== Original Object ===");
console.log(person);

// ------------------------------------------------------------------
// Access properties
// ------------------------------------------------------------------

console.log("\n=== Access Properties ===");

console.log(person.firstName);
console.log(person.lastName);
console.log(person.age);

// ------------------------------------------------------------------
// Update a property
// ------------------------------------------------------------------

console.log("\n=== Update Property ===");

const updatedPerson: Person = updateAge(person, 31);

console.log(updatedPerson);

// ------------------------------------------------------------------
// Add optional property
// ------------------------------------------------------------------

console.log("\n=== Add Property ===");

updatedPerson.city = "New York";

console.log(updatedPerson);

// ------------------------------------------------------------------
// Function Example
// ------------------------------------------------------------------

console.log("\n=== Function Example ===");

const summary: string = getPersonSummary(updatedPerson);

console.log(summary);

// ------------------------------------------------------------------
// Object.keys()
// ------------------------------------------------------------------

console.log("\n=== Object.keys() ===");

const keys: string[] = Object.keys(updatedPerson);

console.log(keys);

// ------------------------------------------------------------------
// Object.values()
// ------------------------------------------------------------------

console.log("\n=== Object.values() ===");

const values: unknown[] = Object.values(updatedPerson);

console.log(values);

// ------------------------------------------------------------------
// Object.entries()
// ------------------------------------------------------------------

console.log("\n=== Object.entries() ===");

for (const [key, value] of Object.entries(updatedPerson)) {
    console.log(`${key}: ${value}`);
}

// ------------------------------------------------------------------
// Property Checks
// ------------------------------------------------------------------

console.log("\n=== Property Checks ===");

console.log(Object.hasOwn(updatedPerson, "age"));
console.log(Object.hasOwn(updatedPerson, "salary"));

// ------------------------------------------------------------------
// Clone an Object
// ------------------------------------------------------------------

console.log("\n=== Clone Object ===");

const clonedPerson: Person = {
    ...updatedPerson
};

console.log(clonedPerson);

// ------------------------------------------------------------------
// Freeze an Object
// ------------------------------------------------------------------

console.log("\n=== Object.freeze() ===");

const settings: Readonly<{
    theme: string;
    language: string;
}> = Object.freeze({
    theme: "dark",
    language: "en"
});

console.log(settings);

// ------------------------------------------------------------------
// Destructuring
// ------------------------------------------------------------------

console.log("\n=== Destructuring ===");

const {
    firstName,
    age
}: {
    firstName: string;
    age: number;
} = updatedPerson;

console.log(firstName);
console.log(age);


/*
run:

=== Original Object ===
{ firstName: 'John', lastName: 'Doe', age: 30, isEmployed: true }

=== Access Properties ===
John
Doe
30

=== Update Property ===
{ firstName: 'John', lastName: 'Doe', age: 31, isEmployed: true }

=== Add Property ===
{
  firstName: 'John',
  lastName: 'Doe',
  age: 31,
  isEmployed: true,
  city: 'New York'
}

=== Function Example ===
John Doe is 31 years old.

=== Object.keys() ===
[ 'firstName', 'lastName', 'age', 'isEmployed', 'city' ]

=== Object.values() ===
[ 'John', 'Doe', 31, true, 'New York' ]

=== Object.entries() ===
firstName: John
lastName: Doe
age: 31
isEmployed: true
city: New York

=== Property Checks ===
true
false

=== Clone Object ===
{
  firstName: 'John',
  lastName: 'Doe',
  age: 31,
  isEmployed: true,
  city: 'New York'
}

=== Object.freeze() ===
{ theme: 'dark', language: 'en' }

=== Destructuring ===
John
31

*/

 



answered Oct 16 by avibootz
...