面试官提问:TypeScript 中的 Type 和 Interface 有什么区别?

两者的共同点

1、interface 和 type 都可以用来声明一个对象

interface 声明对象

js 复制代码
interface UserInterFace {
  name: string;
  age: number;
  getName: () => string
}

const user: UserInterFace = {
  name: 'xx',
  age: 20,
  getName() { return 'xx' }
}

type 声明对象

js 复制代码
type UserType = {
  name: string;
  age: number;
  getName: () => string;
}

const user: UserType = {
  name: 'xx',
  age: 20,
  getName() { return 'xx' }
}

2、interface 和 type 都可以被 class 实现

interface 声明对象,被 class 实现

js 复制代码
interface UserInterFace {
  name: string;
  age: number;
  getName: () => string
}

class User implements UserInterFace {
  name: string;
  age: number;
  getName: () => string;
}

type 声明对象,被 class 实现

js 复制代码
type UserType = {
  name: string;
  age: number;
  getName: () => string;
}

class User implements UserInterFace {
  name: string;
  age: number;
  getName: () => string;
}

3、interface 和 type 都可以被扩展

interface 扩展 interface 和 type

js 复制代码
interface User1 { name: string } // 扩展 interface
type User1 = { name: string } // 扩展 type


interface User2 extends User1 { age: number } 
const u2: User2 = { name: 'xxx', age: 12 }

type 扩展 type 和 interface

js 复制代码
type T1 = { name: string } // 扩展 type
interface T1 = { name: string } // 扩展 interface

type T3 = T1 & {
   age: number
}; 

const obj2: T3 = { name: 'xxx', age: 123 }

两者的不同点

1、type 可以声明基础类型

js 复制代码
type name = string
const str1: name = 'xxx'; // 合法
const str2: name = 12; // 不合法 

2、type 可以声明联合类型、交叉类型

js 复制代码
type T1 = { name: string }
type T2 = { age: number }

type T3 = T1 | T2; // 联合类型
type T4 = T1 & T2; // 交叉类型
const obj1: T3 = { name: 'xxx' }
const obj2: T4 = { name: 'xxx', age: 123 }

变种:interface 可以参与到 联合类型交叉类型

js 复制代码
type T1 = { name: string }
interface T2 { age: number }

type T3 = T1 | T2; // 联合类型
type T4 = T1 & T2; // 交叉类型
const obj1: T3 = { name: 'xxx' }
const obj2: T4 = { name: 'xxx', age: 123 }

3、interface 可以重复声明

interface:支持 ‌声明合并 ‌:多次声明同名 interface 会自动合并‌

typescript 复制代码
interface User { name: string; }
interface User { age: number; }
// 合并为 { name: string; age: number; }

type:不支持声明合并‌:重复定义同名 type 会报错‌

typescript 复制代码
type User = { name: string; };
type User = { age: number; }; // Error: Duplicate identifier

4、implements 实现的不同

interface: 类可以直接通过 implements 实现一个 interface

typescript 复制代码
class Admin implements User { /* ... */ }

type 如果 type 定义的是 ‌对象类型或函数签名‌,类也可以实现

typescript 复制代码
type UserType = { name: string };
class Admin implements UserType { /* 合法 */ }

type MixedType = string | number;
class Foo implements MixedType {} // Error

5、对于复杂类型的支持程度

type:可处理 ‌联合类型 ‌、‌交叉类型 ‌、‌元组 ‌ 等复杂类型,且支持工具类型操作(如 PartialPick)‌

typescript 复制代码
type ErrorCode = string | number;
type PartialUser = Partial<User>;

interface‌:无法直接定义联合类型或元组,需通过组合其他类型实现‌

如何选择

  • 优先使用interface:当需要 ‌声明对象类型 ‌ 或 ‌利用声明合并‌ 特性时。
  • 优先使用 ‌type:当需要定义 ‌联合类型、元组、工具类型 ‌ 或 ‌复杂类型操作‌ 时
相关推荐
为了摸鱼而战13 分钟前
OpenSpec + Superpowers + gstack 三器合一:你的 AI 编程终于可以从"拍脑袋"进化到"流水线"了
前端
程序猿乐锅18 分钟前
【苍穹外卖 day11|统计报表接口与 Apache ECharts 图表展示】
前端·apache·echarts
yy403318 分钟前
【HarmonyOS学习笔记】2026-07-19 | 布局性能实验:百分比vs固定值vs预计算
前端·harmonyos
渣波19 分钟前
🚀 全栈AI革命:用 Node.js + LangChain + dotenv 打造你的智能应用基座
前端
程序员Jason21 分钟前
Node.js 极简安装指南(Mac / Windows / Linux 通用,含国内镜像)
前端
半个落月24 分钟前
Vue 3 如何接住大模型的流式回答:从 ReadableStream 到可靠的 SSE 解析
前端·javascript·人工智能
蓝银草同学26 分钟前
Stream 数据统计实战:求和、平均值、分组汇总(AI 辅助学习 Java 8)
java·前端·后端
Dontla27 分钟前
Hero Section(首屏大图区 / 英雄区)介绍(Web网页落地页Landing Page最顶部的区域)
前端
问商十三载1 小时前
2026大模型GEO优化体系:3层链路提收录,零成本提34%引用率附工具包
大数据·前端·人工智能