Object Types and Classes
An object type lists property names with their types, and a class declaration gives you both a constructor and a type of the same name.
Describe object fields and class instances without confusing a type with a runtime object.
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 propertyThree rules to know:
- Required by default. Leaving out
titleis an error: the property is missing. ?makes a property optional.duemay be absent, so reading it gives astringorundefined.- Excess property check. When you write a fresh object literal where a
Taskis expected, TypeScript also flags properties the type does not mention, such aspriority: 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