Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
TypeScript Types  ›  Lesson

Object Types and Classes

Object 12 minAn object type lists property names with their types, and a class declaration gives you both a constructor and a type of the same name.

Object Types and Classes

Our tracker's central value is a task: an object with an id, a title, a done flag and maybe a due date. Describing that shape once, and having the compiler hold every function to it, is where TypeScript starts to pay for itself.

The rule

An object type lists each property and its type; a class is also a type.

Write an object type with braces, listing each property name and its type:

type Task = {
  readonly id: number;    // cannot be reassigned
  title: string;
  done: boolean;
  due?: string;           // optional: may be left out
};

const t: Task = { id: 1, title: "Buy milk", done: false };

t.done = true;   // fine
t.id = 2;        // error: id is a read-only property

Three rules to know:

  • Required by default. Leaving out title is an error: the property is missing.
  • ? makes a property optional. due may be absent, so reading it gives a string or undefined.
  • Excess property check. When you write a fresh object literal where a Task is expected, TypeScript also flags properties the type does not mention, such as priority: 1. Those are usually typos.

You can also name an object shape with an interface, which the final section covers in depth. For now, type Task = {...} and interface Task {...} both describe the same shape.

Avoid the loose types Object, object and {} for data like this. They say almost nothing: you cannot read .title from a value typed object.

Classes. A class declaration creates two things: a constructor you call at runtime, and a type with the same name describing its instances.

class TaskList {
  tasks: Task[] = [];
  add(task: Task): void { this.tasks.push(task); }
}

const inbox: TaskList = new TaskList(); // TaskList used as a type

The shortcut

For every property, ask two questions: "Is it always there?" (no → ?) and "Can it change after creation?" (no → readonly). Everything else is a plain name: type.

Typing a task, property by propertyStep 1 of 4
  1. id

    Always there, never changes: readonly id: number.

The trap: readonly is checked at compile time only. It does not freeze the object at runtime.

Drills

Answer fast — trust the rule.

quick drill

A Task type requires title. What happens with const t: Task = { id: 1, done: false };?

quick drill

How do you mark due as a property that may be left out?

quick drill

Using the TaskList class above, which line is valid?

quick drill

Why does { id: 1, title: "x", done: false, prio: 2 } fail when assigned directly to a Task?

Remember

  • An object type lists each property and its type; a class is also a type.
  • Ask "always there?" for ? and "can it change?" for readonly.
  • Trap: readonly and other types vanish at runtime; they do not freeze objects.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainShape of a task

Write an object type for a task with an id, a title, a done flag and an optional due date string. Then explain what happens if you create a task literal that misses the title, and one that adds an extra priority property.