🧪 Frontend Lab
交互式前端学习实验室

Interface 接口定义

typescript · beginner · 15-20 min · Step 1/3

TypeScript Interface

interface User {
  name: string;
  age: number;
  email?: string;      // 可选属性
  readonly id: number;  // 只读属性
}

const user: User = { id: 1, name: 'Alice', age: 25 };

接口继承

interface Employee extends User {
  department: string;
  salary: number;
}

函数类型接口

interface SearchFunc {
  (query: string, limit?: number): string[];
}

Interface vs Type

特性interfacetype
扩展extends& 交叉
声明合并支持不支持
联合类型不支持支持
定义对象结构优先用 interface,需要联合/交叉类型用 type。