Most tests need a book. Few of them care about every field, so a suite has a builder: a function that returns a complete book and lets a test change the fields it cares about.
The parameter needs a type. It is "a book, with every field optional". One way is a second interface.
interface Book {
id: number;
title: string;
copies: number;
}
interface BookOverrides {
id?: number;
title?: string;
copies?: number;
}
It works on the day you write it. Then someone adds shelf to Book, and nobody remembers the second interface. Now no test can override the shelf, and no error says why.
TypeScript can make the second type from the first.
Partial
interface Book {
id: number;
title: string;
copies: number;
}
function buildBook(overrides: Partial<Book> = {}): Book {
return { id: 1, title: "Dune", copies: 3, ...overrides };
}
console.log(buildBook());
console.log(buildBook({ copies: 0 }));
{"id":1,"title":"Dune","copies":3}
{"id":1,"title":"Dune","copies":0}
Partial<Book> is Book with every field optional. It is a utility type: a generic type that TypeScript provides, which takes a type and gives back a changed one. Add a field to Book and Partial<Book> has it too.
A typing mistake in an override is still an error, because the fields are still the fields of Book.
interface Book {
id: number;
title: string;
copies: number;
}
function buildBook(overrides: Partial<Book> = {}): Book {
return { id: 1, title: "Dune", copies: 3, ...overrides };
}
buildBook({ copys: 0 });
books.ts(11,13): error TS2353: Object literal may only specify known properties, and 'copys' does not exist in type 'Partial<Book>'.
Omit and Pick
The server chooses the id of a new book. So the data a test sends to create one is a book with no id.
interface Book {
id: number;
title: string;
copies: number;
}
type NewBook = Omit<Book, "id">;
type BookRow = Pick<Book, "id" | "title">;
const toCreate: NewBook = { title: "Emma", copies: 2, id: 9 };
books.ts(10,55): error TS2353: Object literal may only specify known properties, and 'id' does not exist in type 'NewBook'.
Omit<Book, "id"> is Book without that field. Pick<Book, "id" | "title"> is the opposite: only the fields you name. The names are a union of literal types, so a field that does not exist is an error too.
Record
Record<Keys, Value> is an object with one entry for each key. It is most useful when the keys are a union.
type LoanStatus = "on-loan" | "returned" | "overdue";
const labels: Record<LoanStatus, string> = {
"on-loan": "On loan",
returned: "Returned",
};
labels.ts(3,7): error TS2741: Property 'overdue' is missing in type '{ "on-loan": string; returned: string; }' but required in type 'Record<LoanStatus, string>'.
The object must have a label for every status. When the application gets a fourth status, you add it to the union, and the compiler lists every table that needs a new entry. Teams often keep expected texts in tables like this one.
What Partial does not protect
Here is the builder again, with one override.
interface Book {
id: number;
title: string;
copies: number;
}
function buildBook(overrides: Partial<Book> = {}): Book {
return { id: 1, title: "Dune", copies: 3, ...overrides };
}
const book = buildBook({ copies: undefined });
console.log(book.copies);
undefined
The return type says copies is a number. The value is undefined, and the compiler reported nothing.
With the settings this course uses, an optional field accepts undefined written out. Lecture 11 names the setting that changes this. The spread then copies that undefined over the default. This happens in real suites when an override comes from a variable that is sometimes empty. The builder's return type is a claim about every call, and this call breaks it.
What Readonly does not protect
Readonly<Book> marks every field as not assignable. It suits test data that several tests share.
interface Book {
title: string;
shelf: { floor: number };
}
const shared: Readonly<Book> = { title: "Dune", shelf: { floor: 2 } };
shared.title = "Emma";
shared.ts(7,8): error TS2540: Cannot assign to 'title' because it is a read-only property.
That is the protection. It has two limits.
interface Book {
title: string;
shelf: { floor: number };
}
const shared: Readonly<Book> = { title: "Dune", shelf: { floor: 2 } };
shared.shelf.floor = 9;
console.log(shared.shelf.floor);
9
Readonly covers one level. shelf cannot be replaced, and the object inside it can still be changed. And Readonly is a type, so it is removed before the code runs. It stops a mistake in code the compiler checks, and nothing else.
Remember this
Partial, Omit and Pick build a type from one you already have, so the two cannot drift apart. Record over a union makes the compiler ask for every entry. Partial still lets undefined through, and Readonly covers one level and only at compile time.
Reference: Utility Types in the TypeScript Handbook.