WHAT'S NEW
TypeScript · Lesson 06 of 11

Understanding Objects

Give your data structure and meaning. Learn how to group related values into objects, define their types, and build reusable developer profiles.

Properties Object Types Optional Values Methods
● ● ●   objects.ts
const developer = {
  name: "Moe",
  language: "TypeScript",
  level: 6
};

console.log(
  developer.name + " learns "
  + developer.language
);
CONSOLE OUTPUT
> Ready to run…
01 / Meet Objects

Keep Related Information Together

In the previous lesson, you learned how arrays store collections of values. But what happens when you want to describe one person, product, or project using several different pieces of information?

An object groups related values using named properties. Each property has a name and a value.

developer.ts
const developer = {
  name: "Moe",
  language: "TypeScript",
  level: 6
};

console.log(developer.name);
Console Output:
Moe

Here, name, language, and level are properties. Together, they describe one developer.

02 / Reading Properties

Access Data Using Property Names

You can read object properties using dot notation or bracket notation.

access-properties.ts
const developer = {
  name: "Moe",
  language: "TypeScript",
  level: 6
};

console.log(developer.name);
console.log(developer["language"]);
Console Output:
Moe TypeScript

Dot notation is convenient when you know the property name. Bracket notation is useful when working with property names stored in variables.

Remember: Arrays use numeric indexes to locate elements. Objects use property names to locate values.
03 / Updating Properties

Change Information Inside an Object

Object properties can be updated when they are writable, even if the object itself was declared using const.

update-object.ts
const developer = {
  name: "Moe",
  level: 6
};

developer.level = 7;

console.log(developer.level);
Console Output:
7

TypeScript infers the original property types. Since level starts as a number, assigning a string to it would produce a type error.

04 / Explicit Object Types

Describe Exactly What an Object Needs

TypeScript allows you to specify the required properties and their types. This gives your objects a predictable structure.

typed-object.ts
const developer: {
  name: string;
  language: string;
  level: number;
} = {
  name: "Moe",
  language: "TypeScript",
  level: 6
};

console.log(developer.name);

TypeScript checks that the object provides the expected properties and values.

Incorrect property type
const developer: {
  name: string;
  level: number;
} = {
  name: "Moe",
  level: "six" // TypeScript error
};

The value "six" is a string, but the level property requires a number.

05 / Type Aliases

Give an Object Structure a Name

Writing the same object type repeatedly can make code difficult to maintain. A type alias lets you give a type a reusable name.

type-alias.ts
type Developer = {
  name: string;
  language: string;
  level: number;
};

const moe: Developer = {
  name: "Moe",
  language: "TypeScript",
  level: 6
};

console.log(moe.language);
Console Output:
TypeScript

Now you can use Developer whenever you need another object with the same structure.

Tip: Type aliases are a TypeScript feature. They help the compiler check your code but do not create JavaScript objects at runtime.
06 / Optional Properties

Make Some Information Optional

Not every object needs every possible property. Add a question mark after a property name to make it optional.

optional-properties.ts
type Developer = {
  name: string;
  language: string;
  website?: string;
};

const moe: Developer = {
  name: "Moe",
  language: "TypeScript"
};

console.log(moe.name);

The website property can be omitted. When accessed, an absent optional property has the value undefined.

Safely reading an optional property
if (moe.website !== undefined) {
  console.log(moe.website);
} else {
  console.log("No website added yet.");
}
Console Output:
No website added yet.
07 / Readonly Properties

Protect Values from Reassignment

The readonly modifier tells TypeScript that a property should not be reassigned after the object is created.

readonly.ts
type Developer = {
  readonly id: number;
  name: string;
};

const moe: Developer = {
  id: 101,
  name: "Moe"
};

moe.name = "Moe Merheb";

// TypeScript error:
// moe.id = 202;

TypeScript prevents reassignment of id during type checking. However, readonly alone does not freeze the object at runtime.

08 / Object Methods

Give Your Objects Actions

An object can contain functions as properties. These functions are commonly called methods.

methods.ts
const developer = {
  name: "Moe",
  greet(): void {
    console.log("Hello from " + this.name);
  }
};

developer.greet();
Console Output:
Hello from Moe

The this keyword refers to the object when the method is called as developer.greet().

Good habit: Regular method syntax works well when you need this to refer to the object. Arrow functions handle this differently.
09 / Arrays of Objects

Combine Collections with Structured Data

Objects and arrays work beautifully together. You can create an array where every element follows the same object type.

developers.ts
type Developer = {
  name: string;
  level: number;
};

const developers: Developer[] = [
  { name: "Moe", level: 6 },
  { name: "Jordan", level: 4 }
];

for (const developer of developers) {
  console.log(
    developer.name + ": level " + developer.level
  );
}
Console Output:
Moe: level 6 Jordan: level 4

This pattern is useful for lists of users, products, lessons, and other structured information.

10 / Objects and Functions

Pass Structured Information to Functions

Functions can accept entire objects as parameters. TypeScript checks that the object contains the required information.

developer-summary.ts
type Developer = {
  name: string;
  language: string;
  level: number;
};

function describeDeveloper(person: Developer): void {
  console.log(
    person.name + " is learning " + person.language
  );
}

describeDeveloper({
  name: "Moe",
  language: "TypeScript",
  level: 6
});
Console Output:
Moe is learning TypeScript

Instead of passing several separate values, you can pass one well-defined object.

🚀 mC0de Challenge

Build Moe’s Developer Profile

Create a TypeScript object that represents a developer and displays a personalized profile.

Your Mission

  1. Create a reusable Developer type with a name, favorite language, and level.
  2. Add an optional website property.
  3. Create a profile for Moe, or replace the name with your own.
  4. Write a function that accepts the developer object and displays its information.
  5. Update the developer’s level and run the program again.
  6. Try assigning a string to the numeric level to see TypeScript catch the mistake.
Extra challenge: Add a greet() method to the object and call it after displaying the profile.
💻 Interactive TypeScript Sandbox

Create Your Developer Profile!

Customize the object, experiment with property types, and run your code. Use Load Solution to see a complete profile with an optional property and an object method.

Console Output
Lesson 06 Recap

What You’ve Learned

You’re Building More Structured Programs!

You can now:

  • Create objects containing related properties.
  • Access and update properties using dot and bracket notation.
  • Define explicit object types.
  • Create reusable object structures using type aliases.
  • Use optional and readonly properties.
  • Write object methods.
  • Combine objects with arrays and functions.

Next up: Interfaces — discover another powerful way to describe and reuse object structures in TypeScript!

Your Learning Journey

0 / 11 Completed

Keep going! You’re more than halfway through your TypeScript learning journey.

🎉

More Than Halfway There!

You’ve learned how to create TypeScript objects, define reusable types, work with optional and readonly properties, and build methods. You’re developing the skills to organize real-world information in code!

PIX0
</> {} JS 01
0101 </> CSS JS HTML PIX0 const {} 1010 async mC0de function return 0101 <div> 1010 PIX0 CSS JS HTML const {} 0101 mC0de
PIX0 // WORLD OF CODE
PIX0 IS PREPARING SOMETHING AWESOME FOR YOU 0%