0Pricing
Frontend Academy · Lesson

Interfaces and Object Types

Describe object shapes with interface and type, extend interfaces, use optional properties with ?, and readonly for immutability.

interface vs type

Both interface and type describe object shapes. The key difference: interfaces can be merged (declaration merging) and are generally preferred for defining public API shapes. type aliases are more flexible for complex types.

// interface:
interface User {
  id: number;
  name: string;
}

// type alias (equivalent for objects):
type User = {
  id: number;
  name: string;
};

Defining an Interface

Use interface to define the shape of an object. All properties are required by default. Separate properties with semicolons.

interface Product {
  id: number;
  name: string;
  price: number;
  description: string;
  inStock: boolean;
  category: string;
}

All lessons in this course

  1. Why TypeScript: Types Catch Bugs at Compile Time
  2. Primitive Types Unions and Type Aliases
  3. Interfaces and Object Types
  4. Compiling TS and tsconfig.json
← Back to Frontend Academy