0Pricing
TypeScript Academy · Lesson

Defining Object Shapes with Interfaces

Use interface to describe object structure.

Defining Object Shapes with Interfaces is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

Interfaces describe the shape of objects in TypeScript. They define which properties exist, their types, and whether they are optional or readonly.

Basic Interface

Define an interface with the `interface` keyword followed by property names and their types.
interface User {
  id: number;
  name: string;
  email: string;
}
const alice: User = { id: 1, name: 'Alice', email: 'a@b.com' };

Optional Properties with ?

Mark properties optional with `?`. An optional property may be present or absent.
interface Config {
  host: string;
  port?: number;     // optional
  timeout?: number;  // optional
}

Readonly Properties

Readonly properties can be set once (at creation) but not changed afterwards.
interface Point {
  readonly x: number;
  readonly y: number;
}
const p: Point = { x: 5, y: 10 };
// p.x = 0; // Error!

Method Signatures

Interfaces can include method signatures that describe the function shape.
interface Animal {
  name: string;
  speak(): string;
  move(distance: number): void;
}

Index Signatures

Use an index signature when the object has dynamic keys of a specific type.
interface StringMap {
  [key: string]: string;
}
const headers: StringMap = {
  'content-type': 'application/json',
  'accept': 'text/html',
};

Structural Typing

TypeScript uses structural typing — an object satisfies an interface as long as it has the required properties, regardless of how it was created.
interface Named { name: string; }
function greet(obj: Named) { console.log(obj.name); }
greet({ name: 'Alice', age: 30 }); // Extra props OK when not a literal

Excess Property Checking

TypeScript reports an error when an object literal has extra properties not in the interface. This is excess property checking.
interface Config { host: string; }
// const c: Config = { host: 'localhost', extra: 1 }; // Error on literal
const obj = { host: 'localhost', extra: 1 };
const c: Config = obj; // OK — not a fresh literal

Interface for Function Types

Interfaces can describe callable types with a call signature.
interface Greeter {
  (name: string): string;
}
const greet: Greeter = name => 'Hello, ' + name;

Nested Interfaces

Properties can reference other interfaces to build nested object shapes.
interface Address { street: string; city: string; }
interface Person { name: string; address: Address; }
const p: Person = { name: 'Alice', address: { street: '1 Main', city: 'NY' } };

Interface vs Class Instance

Interfaces describe the public shape of a class but are erased at runtime. The class provides the implementation.
interface Saveable { save(): Promise<void>; }
class UserService implements Saveable {
  async save() { /* ... */ }
}

Quick Check

What happens when you assign an object literal with an extra property to an interface type in TypeScript?

Recap

Interfaces define object shapes with typed properties, optional/readonly modifiers, method signatures, and index signatures. TypeScript uses structural typing — shape compatibility matters, not declarations.

Frequently asked questions

Is the “Defining Object Shapes with Interfaces” lesson free?

Yes — the full text of “Defining Object Shapes with Interfaces” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Defining Object Shapes with Interfaces”?

Use interface to describe object structure. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Defining Object Shapes with Interfaces” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining Object Shapes with Interfaces
  2. Type Aliases for Complex Types
  3. Interface Extension and Merging
  4. Interface vs Type: When to Use Each
← Back to TypeScript Academy