A test builds a book and reads one field back. The field name has two letters swapped.
const book = { title: "Dune", copies: 3 };
console.log(book.titel);
undefined
JavaScript gives undefined for a field that does not exist. The test goes on with undefined, and the failure appears somewhere else.
TypeScript reads the object and remembers its fields.
const book = { title: "Dune", copies: 3 };
console.log(book.titel);
book.ts(2,18): error TS2551: Property 'titel' does not exist on type '{ title: string; copies: number; }'. Did you mean 'title'?
The part in curly brackets is an object type. It lists each field with its type. TypeScript inferred it from the object on line 1.
Naming a shape
Test code passes the same kind of object to many helpers. Writing the object type in each one would repeat it. An interface gives the shape a name.
interface Book {
title: string;
copies: number;
}
function copiesLabel(book: Book): string {
return book.title + ": " + book.copies;
}
const label: string = copiesLabel({ title: "Dune", copies: 3 });
interface Book starts the description. Each line inside names one field and its type. From then on Book is a type, and you use it like string or number.
Three things can go wrong with the object a caller passes. The compiler has a different message for each.
A field with the wrong type
interface Book {
title: string;
copies: number;
}
function copiesLabel(book: Book): string {
return book.title + ": " + book.copies;
}
copiesLabel({ title: "Dune", copies: "3" });
book.ts(10,30): error TS2322: Type 'string' is not assignable to type 'number'.
The error points at the field, on line 10. The other field is fine.
A missing field
interface Book {
title: string;
copies: number;
}
function copiesLabel(book: Book): string {
return book.title + ": " + book.copies;
}
copiesLabel({ title: "Dune" });
book.ts(10,13): error TS2345: Argument of type '{ title: string; }' is not assignable to parameter of type 'Book'.
Property 'copies' is missing in type '{ title: string; }' but required in type 'Book'.
Every field in an interface is required unless you mark it optional. Lecture 6 shows how.
A field the interface does not know
interface Book {
title: string;
copies: number;
}
function copiesLabel(book: Book): string {
return book.title + ": " + book.copies;
}
copiesLabel({ title: "Dune", copies: 3, isbn: "978-0" });
book.ts(10,41): error TS2353: Object literal may only specify known properties, and 'isbn' does not exist in type 'Book'.
TypeScript makes this check when you write an object directly in the place where a Book is expected. It finds a misspelled field name in test data, which is the mistake this lecture started with.
Objects inside objects
A field can hold another object. Describe it in place, or give it its own interface and use the name.
interface Shelf {
floor: number;
code: string;
}
interface Book {
title: string;
copies: number;
shelf: Shelf;
}
const dune: Book = { title: "Dune", copies: 3, shelf: { floor: 2, code: "SF-4" } };
const floor: number = dune.shelf.floor;
The shape is what counts
Notice that none of the objects above says "I am a Book". TypeScript compares fields. Any object with a title that is text and a copies that is a number fits Book.
You will also see type Book = { title: string; copies: number }. For describing an object it does the same job as an interface. This track uses interface for objects.
What this is worth in a test suite
One interface for your test data is most useful on the day the application changes. Say the field copies is renamed to copiesAvailable. You change the interface, and the compiler lists every test file that still uses the old name. Without the interface you find those files one failing test at a time.
Remember this
An interface names the shape of an object: each field and its type. The compiler checks every object against it and reports a wrong type, a missing field, or a field the interface does not know. It compares the fields, not the name of the object.
Reference: Object Types in the TypeScript Handbook.