Typescript 中的 interface 和 type 到底有什么區(qū)別詳解
interface VS type
大家使用 typescript 總會(huì)使用到 interface 和 type,官方規(guī)范 稍微說(shuō)了下兩者的區(qū)別
- An interface can be named in an extends or implements clause, but a type alias for an object type literal cannot.
- An interface can have multiple merged declarations, but a type alias for an object type literal cannot.
但是沒(méi)有太具體的例子。
明人不說(shuō)暗話,直接上區(qū)別。
相同點(diǎn)
都可以描述一個(gè)對(duì)象或者函數(shù)
interface
interface User { name: string age: number } interface SetUser { (name: string, age: number): void; }
type
type User = { name: string age: number }; type SetUser = (name: string, age: number): void;
都允許拓展(extends)
interface 和 type 都可以拓展,并且兩者并不是相互獨(dú)立的,也就是說(shuō) interface 可以 extends type, type 也可以 extends interface 。 雖然效果差不多,但是兩者語(yǔ)法不同。
interface extends interface
interface Name { name: string; } interface User extends Name { age: number; }
type extends type
type Name = { name: string; } type User = Name & { age: number };
interface extends type
type Name = { name: string; } interface User extends Name { age: number; }
type extends interface
interface Name { name: string; } type User = Name & { age: number; }
不同點(diǎn)
type 可以而 interface 不行
type 可以聲明基本類型別名,聯(lián)合類型,元組等類型
// 基本類型別名 type Name = string // 聯(lián)合類型 interface Dog { wong(); } interface Cat { miao(); } type Pet = Dog | Cat // 具體定義數(shù)組每個(gè)位置的類型 type PetList = [Dog, Pet]
type 語(yǔ)句中還可以使用 typeof 獲取實(shí)例的 類型進(jìn)行賦值
// 當(dāng)你想獲取一個(gè)變量的類型時(shí),使用 typeof let div = document.createElement('div'); type B = typeof div
其他騷操作
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> };
interface 可以而 type 不行
interface 能夠聲明合并
interface User { name: string age: number } interface User { sex: string } /* User 接口為 { name: string age: number sex: string } */
總結(jié)
一般來(lái)說(shuō),如果不清楚什么時(shí)候用interface/type,能用 interface 實(shí)現(xiàn),就用 interface , 如果不能就用 type 。其他更多詳情參看 官方規(guī)范文檔
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JScript中的undefined和"undefined"的區(qū)別
JScript中的undefined和"undefined"的區(qū)別...2007-03-03用javascript作一個(gè)通用向?qū)дf(shuō)明
向?qū)Э梢宰屇愕木W(wǎng)站用戶快速上手使用你的web應(yīng)用,提高網(wǎng)站的吸引力。向?qū)б话惴譃楹脦讉€(gè)步驟,每個(gè)步驟收集一些數(shù)據(jù),并且支持退回功能,所有步驟完成后可以得到每一步的收集結(jié)果。這里給大家展示一種比較通用,靈活且簡(jiǎn)單的向?qū)Э蚣堋?/div> 2011-08-08JS實(shí)現(xiàn)滾動(dòng)條觸底加載更多
這篇文章主要介紹了JS滾動(dòng)條觸底加載更多,需要的朋友可以參考下2019-09-09JavaScript使用DeviceOne開(kāi)發(fā)實(shí)戰(zhàn)(二) 生成調(diào)試安裝包
這篇文章主要介紹了JavaScript使用DeviceOne開(kāi)發(fā)實(shí)戰(zhàn)(二) 生成調(diào)試安裝包的相關(guān)資料,需要的朋友可以參考下2015-12-12使用JavaScript實(shí)現(xiàn)頁(yè)面局部更新的方法總結(jié)
在JavaScript中,Ajax(Asynchronous JavaScript and XML)是一種用于在后臺(tái)與服務(wù)器進(jìn)行異步通信的技術(shù),本文給大家介紹了使用JavaScript實(shí)現(xiàn)頁(yè)面局部更新的三種方法,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12Flexible.js可伸縮布局實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Flexible.js可伸縮布局實(shí)現(xiàn)方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11通過(guò)繼承IHttpHandle實(shí)現(xiàn)JS插件的組織與管理
最近,項(xiàng)目中的用到的Js插件越來(lái)越多,有的是用原生javascript寫的,有的是調(diào)用的jquery插件,頁(yè)面上Js和Css文件的引用也越來(lái)越混亂,而且Js文件之間還有引用先后的依賴關(guān)系2010-07-07最新評(píng)論