关键字 — type

type

This article is extracted from the chat log with AI. Please identify it with caution.

type 是什么? #

在 TypeScript 里,type 用来 定义类型别名(Type Alias)
它的作用是给某个类型起一个名字,方便复用和表达复杂的类型结构。

语法:

type 别名 = 某种类型;

基本用法 #

1. 简单别名 #

type UserID = number;
type Username = string;

let id: UserID = 123;
let name: Username = "Alice";

这里 UserIDUsername 本质上就是 numberstring,只是换了个名字,更有语义化。


2. 联合类型 #

type Status = "success" | "error" | "loading";

let s: Status = "success";  // ✅
let t: Status = "other";    // ❌ 报错

type 可以让你定义一个有限集合的类型。


3. 交叉类型 #

type Person = { name: string };
type Employee = { id: number };

type Staff = Person & Employee;

const staff: Staff = {
  name: "Bob",
  id: 42
};

交叉类型相当于 合并多个类型


4. 函数类型 #

type Add = (a: number, b: number) => number;

const sum: Add = (x, y) => x + y;

这样定义函数类型更直观。


5. 对象类型 #

type User = {
  id: number;
  name: string;
  isAdmin?: boolean;  // 可选属性
};

const u: User = { id: 1, name: "Alice" };

6. 与泛型结合 #

type ApiResponse<T> = {
  code: number;
  data: T;
};

const res: ApiResponse<string> = {
  code: 200,
  data: "ok"
};

type 支持泛型,能写出灵活的类型结构。


type vs interface #

在 TypeScript 里,typeinterface 都能定义对象类型,但它们的特点不同:

特性typeinterface
别名可以给任意类型起别名(基本类型、联合、交叉、函数等)只能定义对象、函数、类的结构
扩展& 做交叉扩展extends 继承
合并不能重复定义同名 type可以重复定义同名 interface,会自动合并
灵活性更通用更适合描述面向对象的结构

举例对比 #

// type 的联合
type Status = "success" | "error";

// interface 不行
// interface Status = "success" | "error"; ❌

// interface 的合并
interface User { id: number; }
interface User { name: string; }

// 最终等价于
// interface User { id: number; name: string; }

👉 总结:

  • 需要联合类型 / 基本类型别名 / 函数类型 → 用 type

  • 需要对象扩展 / 声明合并 / 面向对象设计 → 用 interface


✅ 什么时候用 type #

  1. 需要定义别名

    type ID = string | number;
    type Point = [number, number];  // 元组
    

    👉 type 可以直接给基本类型、联合类型、交叉类型、元组、函数等起别名。

  2. 需要联合类型 / 交叉类型

    type Status = "success" | "error" | "loading";
    type Staff = Person & Employee;
    

    👉 interface 不能直接写这种。

  3. 定义函数类型

    type Callback = (msg: string) => void;
    

    👉 虽然 interface 也能写函数签名,但 type 更简洁直观。

  4. 需要条件类型、工具类型

    type ApiResponse<T> = T extends string ? string : number;
    

    👉 type 可以用在高级类型体操里,interface 不行。


✅ 什么时候用 interface #

  1. 描述对象的结构

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

    👉 用 interface 看起来更“面向对象”。

  2. 需要继承(extends)

    interface Animal { name: string; }
    interface Dog extends Animal { bark(): void; }
    

    👉 type 虽然可以用 & 扩展,但 extends 语义更清晰。

  3. 需要声明合并

    interface User { id: number; }
    interface User { name: string; }
    
    // 最终合并为:
    // interface User { id: number; name: string; }
    

    👉 type 不能做到,interface 在大型项目中更适合多人协作。

  4. 与类(class)结合

    class Person implements User {
      id: number = 1;
      name: string = "Alice";
    }
    

    👉 interfaceclass 的契合度更高。


🔑 实战决策清单 #

  • 要别名、函数、联合/交叉类型 → 用 type

  • 要扩展对象、和类交互、多人协作(声明合并) → 用 interface

  • 如果只是对象结构 → 二者都行,风格统一即可

💡 在很多团队里常见规范是:

  • 默认用 interface 表示对象结构

  • type 处理非对象类型 / 复杂类型组合


要不要我给你画一个 流程图(type vs interface 选择指南)?比如“是否是对象结构?是 → interface,否则 → type”。

本文共 1120 字,创建于 Aug 20, 2025

相关标签: Frontend, TypeScript, ByAI