使用 TypeScript 常见困惑:interface 和 type 的区别是什么?

当我们使用 TypeScript 时,就会用到 interface 和 type,平时感觉他们用法好像是一样的,没啥区别,都能很好的使用,所以也很少去真正的理解它们之间到底有啥区别。
首页 新闻资讯 行业资讯 使用 TypeScript 常见困惑:interface 和 type 的区别是什么?

[[406901]]

当我们使用 TypeScript 时,就会用到 interface 和  type,平时感觉他们用法好像是一样的,没啥区别,都能很好的使用,所以也很少去真正的理解它们之间到底有啥区别。我们开发过经常或这么来定义类型:

复制

interface Point {     x: number;     y: number;
  • 1.

  • 2.

  • 3.

当我们使用 TypeScript 时,就会用到 `interface` 和 `type`,平时感觉他们用法好像是一样的,没啥区别,都能很好的使用,所以也很少去真正的理解它们之间到底有啥区别。我们开发过经常或这么来定义类型:

复制

interface Point { x: number; y: number; }
  • 1.

或者这样定义:

复制

type Point = { x: number; y: number; };
  • 1.

复制

 `interface` 和 `type`之间的差异不仅仅是次要语法声明。那么,今天我们就来看看这两家伙之间存在啥不可告人的秘密。     ### 类型和类型别名     TypeScript 有 `boolean`、`number`、`string` 等基本类型。如果我们想声明高级类型,我们就需要使用**类型别名**。   类型别名指的是为类型创建新名称。**需要注意的是**,我们并没有定义一个新类型。使用`type`关键字可能会让我们觉得是创建一个新类型,但我们只是给一个类型一个新名称。  所以我们所以 type 时,不是在创建新的类别,而是定义类型的一个别名而已。  ### 接口  与 `type`相反,接口仅限于对象类型。它们是描述对象及其属性的一种方式。类型别名声明可用于任何基元类型、联合或交集。**在这方面,接口被限制为对象类型**。    ### interface 和 type 的相似之处  在讨论它们的区别之前,我们先来看看它们的相似之处。  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

或者这样定义:

复制

type Point = {     x: number;     y: number; };
  • 1.

  • 2.

  • 3.

  • 4.

interface 和 type之间的差异不仅仅是次要语法声明。那么,今天我们就来看看这两家伙之间存在啥不可告人的秘密。

类型和类型别名

TypeScript 有 boolean、number、string 等基本类型。如果我们想声明高级类型,我们就需要使用类型别名。

类型别名指的是为类型创建新名称。需要注意的是,我们并没有定义一个新类型。使用type关键字可能会让我们觉得是创建一个新类型,但我们只是给一个类型一个新名称。

所以我们所以 type 时,不是在创建新的类别,而是定义类型的一个别名而已。

接口

与  type相反,接口仅限于对象类型。它们是描述对象及其属性的一种方式。类型别名声明可用于任何基元类型、联合或交集。在这方面,接口被限制为对象类型。

interface 和 type 的相似之处

在讨论它们的区别之前,我们先来看看它们的相似之处。

两者都可以被继承

interface 和 type 都可以继承。另一个值得注意的是,接口和类型别名并不互斥。类型别名可以继承接口,反之亦然。

对于一个接口,继承另一个接口

复制

interface PartialPointX { x: number; } interface Point extends PartialPointX { y: number; }
  • 1.

  • 2.

或者,继承一个类型

复制

type PartialPointX = { x: number; }; interface Point extends PartialPointX { y: number; }
  • 1.

  • 2.

类型继承另一个类型:

复制

type PartialPointX = { x: number; }; type Point = PartialPointX & { y: number; };
  • 1.

  • 2.

或者,继承一个接口:

复制

interface PartialPointX { x: number; } type Point = PartialPointX & { y: number; };
  • 1.

  • 2.

实现

类可以实现接口以及类型(TS 2.7+)。但是,类不能实现联合类型。

复制

interface Point {  x: number;  y: number; }  class SomePoint implements Point {  x = 1;  y = 2; }  type AnotherPoint = {  x: number;  y: number; };  class SomePoint2 implements AnotherPoint {  x = 1;  y = 2; }  type PartialPoint = { x: number; } | { y: number; };  // Following will throw an error class SomePartialPoint implements PartialPoint {  x = 1;  y = 2; }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

interface 和 type 的区别

并集和交集类型

虽然接口可以被扩展和合并,但它们不能以联合和交集的形式组合在一起。类型可以使用联合和交集操作符来形成新的类型。

复制

// object type PartialPointX = { x: number; }; type PartialPointY = { y: number; };  // 并集 type PartialPoint = PartialPointX | PartialPointY;  // 交集 type PartialPoint = PartialPointX & PartialPointY;
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

声明合并

TypeScript编译器合并两个或多个具有相同名称的接口。这不适用于类型。如果我们尝试创建具有相同名称但不同的属性的两种类型,则TypeScript编译器将抛出错误。

复制

// These two declarations become: // interface Point { x: number; y: number; } interface Point { x: number; } interface Point { y: number; }  const point: Point = { x: 1, y: 2 };
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

元组类型

元组(键值对)只能通过type关键字进行定义。

复制

type Point = [x: number, y: number];
  • 1.

没有办法使用接口声明元组。不过,我们可以在接口内部使用元组

复制

interface Point {   coordinates: [number, number] }
  • 1.

  • 2.

  • 3.

我们应该使用哪一个?

一般来说,接口和类型都非常相似。

对于库或第三方类型定义中的公共API定义,应使用接口来提供声明合并功能。除此之外,我们喜欢用哪个就用哪个,但是在整个代码库中应该要保持一致性。

~完,我是小智。

作者:SARANSH KATARIA

译者:前端小智

来源:wisdomgeek

原文:https://www.wisdomgeek.com/development/web-development/typescript/typescript-the-difference-between-interface-and-type/

 

40    2021-06-23 08:01:18    TypeScript interface type