Objects, interfaces and structural typing
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Choose between an interface and a type alias with a reason.
- Use optional, readonly and index signature members correctly.
- Explain excess property checks and structural compatibility.
Overview
#Interfaces and type aliases
Both describe object shapes. Use an interface for objects that may be extended or implemented; use a type for unions, tuples and computed types.
interface User {
id: string;
name: string;
nickname?: string;
readonly createdAt: number;
}
Optional and readonly
nickname?: string means the property may be absent. readonly blocks reassignment after creation; it is a compile-time rule, not a runtime freeze.
Structural typing
Compatibility is by shape, not by name. Any object with the required members satisfies the type.
const u: User = { id: "1", name: "Ada", createdAt: 0 };
Excess property checks
A fresh object literal is checked more strictly: unknown properties are rejected.
const u: User = { id: "1", name: "Ada", createdAt: 0, age: 3 }; // error
Assigning through a variable skips the check because the variable already has a type.
Index signatures
When keys are dynamic, declare the shape of the values:
interface Scores {
[topic: string]: number;
}
Keep index signatures honest: every declared member must match the value type.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Does this compile? ```ts interface User { id: string } const u: User = { id: "1", admin: true }; ```
Exercise 2Implement
Define `type Settings = Readonly<{ theme: "light" | "dark"; fontSize: number }>` and implement `withFontSize(settings: Settings, size: number): Settings` that returns a new object with the updated size.
starter.typescriptexport type Settings = Readonly<{ theme: "light" | "dark"; fontSize: number }>; export function withFontSize(settings: Settings, size: number): Settings { // your code }Reveal the reference answer
reference.typescriptexport type Settings = Readonly<{ theme: "light" | "dark"; fontSize: number }>; export function withFontSize(settings: Settings, size: number): Settings { return { ...settings, fontSize: size }; }Authored expected output: Compiles with no errors. Runtime: withFontSize({ theme: "dark", fontSize: 12 }, 16).fontSize is 16.
readonly prevents mutation in place, so the update returns a new object.
Exercise 3Debug
This fails to compile. Explain the cause and fix it without removing readonly. ```ts interface Config { readonly retries: number } const c: Config = { retries: 1 }; c.retries = 2; ```
starter.typescriptinterface Config { readonly retries: number } const c: Config = { retries: 1 }; c.retries = 2;Reveal the reference answer
reference.typescriptinterface Config { readonly retries: number } const c: Config = { retries: 1 }; const updated: Config = { ...c, retries: 2 };Authored expected output: Compile error resolved: Cannot assign to 'retries' because it is a read-only property. The new object is valid.
readonly blocks direct assignment; create a new object to change the value.
Exercise 4Explain
Give one situation where an interface is the better choice and one where a type alias is.
Reveal the reference answer
reference.typescriptUse an interface for a public object contract that other code will extend or a class will implement, because interfaces support declaration merging and are readable in errors. Use a type alias for unions, tuples, mapped or conditional types, which interfaces cannot express.Authored expected output: A written comparison naming extension and union support.
The decision follows from what each construct can represent.
Common misconceptions
#- readonly freezes the object at runtime.
- An interface and a type alias are always interchangeable.
- Excess property checks apply to every assignment.
Follow-up practice
#- typescript-type-guards-discriminated
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.