偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

TypeScript 中 Type 和 Interface 有什么區(qū)別?

開發(fā) 前端
今天我們來看看 Type 和 Interface 的區(qū)別。Type 是 類型別名,給一些類型的組合起別名,這樣能夠更方便地在各個(gè)地方使用。Interface 是 接口。有點(diǎn)像 type,可以用來代表一種類型組合,但它范圍更小一些,只能描述對(duì)象結(jié)構(gòu)。

大家好,我是前端西瓜哥,今天我們來看看 type 和 interface 的區(qū)別。

type 和 interface

type 是 類型別名,給一些類型的組合起別名,這樣能夠更方便地在各個(gè)地方使用。

假設(shè)我們的業(yè)務(wù)中,id 可以為字符串或數(shù)字,那么我們可以定義這么一個(gè)名為 ID 的 type:

type ID = string | number;

定義一個(gè)名為 Circle 的對(duì)象結(jié)構(gòu) type:

type Circle = {
x: number;
y: number;
radius: number;
}

interface 是 接口。有點(diǎn)像 type,可以用來代表一種類型組合,但它范圍更小一些,只能描述對(duì)象結(jié)構(gòu)。

interface Position {
x: number;
y: number;
}

它們寫法有一點(diǎn)區(qū)別,type 后面需要用 =,interface 后面不需要 =,直接就帶上 {。

范圍

type 能表示的任何類型組合。

interface 只能表示對(duì)象結(jié)構(gòu)的類型。

繼承

interface 可以繼承(extends)另一個(gè) interface。

下面代碼中,Rect 繼承了 Shape 的屬性,并在該基礎(chǔ)上新增了 width 和 height 屬性。

interface Shape {
x: number;
y: number;
}
// 繼承擴(kuò)展
interface Rect extends Shape {
width: number;
height: number;
}
const rect: Rect = { x: 0, y: 0, width: 0, height: 0 };

interface 也可以繼承自 type,但只能是對(duì)象結(jié)構(gòu),或多個(gè)對(duì)象組成交叉類型(&)的 type。

再來看看 type 的繼承能力。

type 可以通過 & 的寫法來繼承 type 或 interface,得到一個(gè)交叉類型:

type Shape = {
x: number;
y: number;
}
type Circle = Shape & { r: number }
const circle: Circle = { x: 0, y: 0, r: 8 }

聲明合并

interface 支持聲明合并,文件下多個(gè)同名的 interface,它們的屬性會(huì)進(jìn)行合并。

interface Point {
x: number;
}
interface Point {
y: number;
}
const point: Point = { x: 10, y: 30 };

需要注意的是,同名屬性的不能進(jìn)行類型覆蓋修改,否則編譯不通過。比如我先聲明屬性 x 類型為 number,然后你再聲明屬性 x 為 string | numebr,就像下面這樣,編譯器會(huì)報(bào)錯(cuò)。

interface Point {
x: number;
}
interface Point {
// 報(bào)錯(cuò)
// Property 'x' must be of type 'number', but here has type 'string | number'.
x: string | number;
y: number;
}

extends 可以將屬性的類型進(jìn)行收窄,比如從 string | number 變成 string。

但聲明合并不行,類型必須完全一致。

type 不支持聲明合并,一個(gè)作用域內(nèi)不允許有多個(gè)同名 type。

// 報(bào)錯(cuò):Duplicate identifier 'Point'.
type Point = {
x: number;
}
// 報(bào)錯(cuò):Duplicate identifier 'Point'.
type Point = {
y: number;
}

當(dāng)然,如果有和 type 同名的 interface,也會(huì)報(bào)錯(cuò)。

結(jié)尾

總結(jié)一下,type 和 interface 的不同點(diǎn)有:

  1. type 后面有 =,interface 沒有。
  2. type 可以描述任何類型組合,interface 只能描述對(duì)象結(jié)構(gòu)。
  3. interface 可以繼承自(extends)interface 或?qū)ο蠼Y(jié)構(gòu)的 type。type 也可以通過 &做對(duì)象結(jié)構(gòu)的繼承。
  4. 多次聲明的同名 interface 會(huì)進(jìn)行聲明合并,type 則不允許多次聲明。

大多數(shù)情況下,我更推薦使用 interface,因?yàn)樗鼣U(kuò)展起來會(huì)更方便,提示也更友好。& 真的很難用。

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2022-03-13 18:53:31

interfacetypeTypeScript

2022-05-06 09:21:21

TypeScriptinterfacetype

2021-06-23 08:01:18

TypeScript interface type

2022-08-31 08:33:54

Bash操作系統(tǒng)Linux

2019-04-03 14:16:25

Type 1Type 2虛擬機(jī)

2021-08-05 08:32:45

TypeScript InterfaceType

2021-03-27 10:56:17

promisethenfinally

2022-09-07 18:32:57

并發(fā)編程線程

2020-03-09 20:56:19

LoRaLoRaWAN無線技術(shù)

2022-06-06 14:53:02

LoRaLoRaWAN

2020-11-09 14:07:53

PyQtQt編程

2022-09-08 18:38:26

LinuxWindowsmacOS

2024-03-05 18:59:59

前端開發(fā)localhost

2022-08-02 08:23:37

SessionCookies

2021-12-17 14:40:02

while(1)for(;;)語言

2022-02-27 15:33:22

安全CASBSASE

2021-05-16 14:26:08

RPAIPACIO

2024-09-09 13:10:14

2024-05-27 00:40:00

2020-08-02 23:20:36

JavaScriptmap()forEach()
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)