夜猫子的知识栈 夜猫子的知识栈
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《Web Api》
    • 《ES6教程》
    • 《Vue》
    • 《React》
    • 《TypeScript》
    • 《Git》
    • 《Uniapp》
    • 小程序笔记
    • 《Electron》
    • JS设计模式总结
  • 《前端架构》

    • 《微前端》
    • 《权限控制》
    • monorepo
  • 全栈项目

    • 任务管理日历
    • 无代码平台
    • 图书管理系统
  • HTML
  • CSS
  • Nodejs
  • Midway
  • Nest
  • MySql
  • 其他
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Ajax
  • Vite
  • Vitest
  • Nuxt
  • UI库文章
  • Docker
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

夜猫子

前端练习生
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《Web Api》
    • 《ES6教程》
    • 《Vue》
    • 《React》
    • 《TypeScript》
    • 《Git》
    • 《Uniapp》
    • 小程序笔记
    • 《Electron》
    • JS设计模式总结
  • 《前端架构》

    • 《微前端》
    • 《权限控制》
    • monorepo
  • 全栈项目

    • 任务管理日历
    • 无代码平台
    • 图书管理系统
  • HTML
  • CSS
  • Nodejs
  • Midway
  • Nest
  • MySql
  • 其他
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Ajax
  • Vite
  • Vitest
  • Nuxt
  • UI库文章
  • Docker
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • TypeScript基础

    • TypeScript笔记
    • tsconfig.json配置
    • Record对象键值定义
    • 对象的方式描述类型
    • implements和extends
    • interface和type
      • 相同点
        • 都可以描述一个对象或者函数
        • interface
        • type
        • 都允许拓展(extends)
        • interface extends interface
        • type extends type
        • interface extends type
        • type extends interface
      • 不同点
        • type 可以而 interface 不行
        • interface 可以而 type 不行
    • keyof和typeof
    • Partial详解
    • 常用泛型工具
  • TypeScript进阶

  • TS 从零实现 axios

  • 《TypeScript》
  • TypeScript基础
夜猫子
2022-09-20
目录

interface和type

# 相同点

# 都可以描述一个对象或者函数

# interface

interface User {
  name: string
  age: number
}

interface SetUser {
  (name: string, age: number): void;
}
1
2
3
4
5
6
7
8

# type

type User = {
  name: string
  age: number
};

type SetUser = (name: string, age: number)=> void;
1
2
3
4
5
6

# 都允许拓展(extends)

interface 和 type 都可以拓展,并且两者并不是相互独立的,也就是说 interface 可以 extends type, type 也可以 extends interface 。 虽然效果差不多,但是两者语法不同。

# interface extends interface

interface Name { 
  name: string; 
}
interface User extends Name { 
  age: number; 
}
1
2
3
4
5
6

# type extends type

type Name = { 
  name: string; 
}
type User = Name & { age: number  };
1
2
3
4

# interface extends type

type Name = { 
  name: string; 
}
interface User extends Name { 
  age: number; 
}
1
2
3
4
5
6

# type extends interface

interface Name { 
  name: string; 
}
type User = Name & { 
  age: number; 
}
1
2
3
4
5
6

# 不同点

# type 可以而 interface 不行

  • type 可以声明基本类型别名,联合类型,元组等类型
// 基本类型别名
type Name = string

// 联合类型
interface Dog {
    wong();
}
interface Cat {
    miao();
}

type Pet = Dog | Cat

// 具体定义数组每个位置的类型
type PetList = [Dog, Pet]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  • type 语句中还可以使用 typeof 获取实例的 类型进行赋值
// 当你想获取一个变量的类型时,使用 typeof
let div = document.createElement('div');
type B = typeof div
1
2
3
  • 其他骚操作
type StringOrNumber = string | number;  
type Text = string | { text: string };  
type NameLookup = Dictionary<string, Person>;  
type Callback<T> = (data: T) => void;  
type Pair<T> = [T, T];  
type Coordinates = Pair<number>;  
type Tree<T> = T | { left: Tree<T>, right: Tree<T> };
1
2
3
4
5
6
7

# interface 可以而 type 不行

interface 能够声明合并

interface User {
  name: string
  age: number
}

interface User {
  sex: string
}

/*
User 接口为 {
  name: string
  age: number
  sex: string 
}
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 总结

一般来说,如果不清楚什么时候用interface/type,能用 interface 实现,就用 interface , 如果不能就用 type 。其他更多详情参看 官方规范文档 (opens new window)

编辑 (opens new window)
上次更新: 2023/7/11 18:57:41
implements和extends
keyof和typeof

← implements和extends keyof和typeof→

最近更新
01
IoC 解决了什么痛点问题?
03-10
02
如何调试 Nest 项目
03-10
03
Provider注入对象
03-10
更多文章>
Copyright © 2019-2025 Study | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式