Interfaces
Describe a shape
An interface lists the members an object must carry. The compiler checks every assigned object against that list and rejects extras of the wrong type and missing members alike:
interface User { id: number; name: string; } const ada: User = { id: 1, name: "Ada" }; console.log(ada.name);
Ada
The ada object matches the User shape member for member. Forgetting the name field or storing text in id would stop the compile with the exact spot named.
Optional members
A question mark marks a member that may be absent. Optional members fit data that appears only in some cases, such as a profile biography:
interface Profile { login: string; bio?: string; } const me: Profile = { login: "ada" }; console.log(me.login);
ada
The object stays valid without a bio, and adding one later is equally fine. Keep required members plain and mark only the truly skippable ones as optional.
Readonly members
The readonly marker locks a single member after creation. Identifiers such as database keys must never change, and the compiler now enforces that rule:
interface Doc { readonly id: number; title: string; } const doc: Doc = { id: 7, title: "Guide" }; console.log(doc.title);
Guide
Reading doc.id works anywhere, while assigning to it fails the compile. Use readonly for identity fields and leave editable content such as title open to change.
Extend interfaces
One interface can extend another to inherit its members and add new ones. Specialized shapes reuse the shared base instead of copying its fields:
interface User { id: number; } interface Admin extends User { level: number; } const boss: Admin = { id: 1, level: 5 }; console.log(boss.level);
5
Admin carries both id from User and its own level. Every Admin still counts as a User, so functions written for User accept admins with no extra work.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does an interface describe?
Answer
The members an object must carry
The interface is a list of members, and the compiler compares every assigned object with it.
-
What does the question mark in
bio?: stringmean?Answer
The member may be absent
An optional member can be left out of the object entirely, while a required one must be there.
-
What does
readonlyon an interface member do?Answer
It allows the value to be read but not assigned again
The value is set when the object is made, and a later assignment fails the build.
-
What does
interface Admin extends Useradd toAdmin?Answer
Every member of
User, plus the members written inAdminThe extending interface inherits the base members, so an
Adminalso satisfiesUser. -
What does this program print?
interface User { id: number; name: string; } const ada: User = { id: 1, name: "Ada" }; console.log(ada.name);Answer
Ada
The object fills both members, and
nameholds the text that is printed. -
What does this program print?
interface Profile { login: string; bio?: string; } const me: Profile = { login: "ada" }; console.log(me.login);Answer
ada
The optional
biomay be absent, so the object is valid andloginprints. -
What does this program print?
interface Doc { readonly id: number; title: string; } const doc: Doc = { id: 7, title: "Guide" }; console.log(doc.title);Answer
Guide
readonlyonly blocks a later assignment, and reading the title is allowed. -
What does this program print?
interface User { id: number; } interface Admin extends User { level: number; } const boss: Admin = { id: 1, level: 5 }; console.log(boss.level);Answer
5
Admininheritsidand addslevel, and the object fills both. -
What does the compiler say about this program?
interface User { id: number; name: string; } const ada: User = { id: 1 }; console.log(ada.name);Answer
error TS2741: Property 'name' is missing in type '{ id: number; }' but required in type 'User'.
Every member without a question mark is required, so the object without
namedoes not match. -
What does the compiler say about this program?
interface Doc { readonly id: number; } const doc: Doc = { id: 7 }; doc.id = 8; console.log(doc.id);Answer
error TS2540: Cannot assign to 'id' because it is a read-only property.
The member is readonly, so the assignment after creation is the error the compiler reports.
Article author: Arthur Isaev