Skip to article content
FrontendXLesson 04 / 40

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.

beginner · about 40 minutes · 4 exercises

  • Predict
  • Explain
  • Implement
  • Debug

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.

snippet.ts
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.

snippet.ts
const u: User = { id: "1", name: "Ada", createdAt: 0 };

Excess property checks

A fresh object literal is checked more strictly: unknown properties are rejected.

snippet.ts
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:

snippet.ts
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.

  1. Exercise 1Predict

    Does this compile? ```ts interface User { id: string } const u: User = { id: "1", admin: true }; ```

    Choose the best answer
  2. 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.typescript
    export type Settings = Readonly<{ theme: "light" | "dark"; fontSize: number }>;
    
    export function withFontSize(settings: Settings, size: number): Settings {
      // your code
    }
    

    Reveal the reference answer
    reference.typescript
    export 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.

  3. 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.typescript
    interface Config { readonly retries: number }
    const c: Config = { retries: 1 };
    c.retries = 2;
    

    Reveal the reference answer
    reference.typescript
    interface 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.

  4. Exercise 4Explain

    Give one situation where an interface is the better choice and one where a type alias is.

    Reveal the reference answer
    reference.typescript
    Use 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

Related resources

#

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.

© 2026 FrontendX. All rights reserved.