面试官提问: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:可处理 ‌联合类型 ‌、‌交叉类型 ‌、‌元组 ‌ 等复杂类型,且支持工具类型操作(如 Partial、Pick)‌

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

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

如何选择

  • 优先使用interface:当需要 ‌声明对象类型 ‌ 或 ‌利用声明合并‌ 特性时。
  • 优先使用 ‌type:当需要定义 ‌联合类型、元组、工具类型 ‌ 或 ‌复杂类型操作‌ 时
相关推荐
子兮曰5 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰5 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万5 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝5 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋5 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁5 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
汉堡大王95275 天前
Jev:不是聊天机器人, 而是一个智能 if 语句
前端·人工智能·后端
梦想很大很大5 天前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端
计算机魔术师5 天前
Meta Muse agent 接入 Shopify 的 Shop Pay 实现代理式购物
前端
沙蒿同学5 天前
我用 Go 搭了一条 AI Agent 流水线:从 1 张商品图到一整套淘宝详情页
前端·javascript·后端