<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
        當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

        Typescript 中的 interface 和 type 到底有什么區(qū)別詳解

        來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 21:55:03
        文檔

        Typescript 中的 interface 和 type 到底有什么區(qū)別詳解

        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
        推薦度:
        導(dǎo)讀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

        interface VS type

        大家使用 typescript 總會(huì)使用到 interface 和 type,官方規(guī)范 稍微說(shuō)了下兩者的區(qū)別

      1. An interface can be named in an extends or implements clause, but a type alias for an object type literal cannot.
      2. An interface can have multiple merged declarations, but a type alias for an object type literal cannot.
      3. 但是沒(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ī)范文檔

        聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Typescript 中的 interface 和 type 到底有什么區(qū)別詳解

        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
        推薦度:
        標(biāo)簽: 中的 的區(qū)別 type
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 久久青青草原国产精品免费| 国产在亚洲线视频观看| 日韩电影免费观看| 亚洲精品中文字幕乱码三区| 巨胸狂喷奶水视频www网站免费| 亚洲狠狠爱综合影院婷婷| 杨幂最新免费特级毛片| 亚洲国产成人精品无码久久久久久综合 | 成人免费无码H在线观看不卡| 亚洲精品美女久久久久99小说| 日韩在线视频播放免费视频完整版| 亚洲精品国精品久久99热| 国产无限免费观看黄网站| 亚洲精品字幕在线观看| 无码专区AAAAAA免费视频| 亚洲成aⅴ人片在线观| 久久久久免费看黄A片APP | 91在线亚洲精品专区| 57PAO成人国产永久免费视频| 亚洲一区精彩视频| 全部免费国产潢色一级| 一区二区3区免费视频| 久久精品国产亚洲夜色AV网站| 97在线视频免费播放| 最新亚洲精品国偷自产在线| 国产免费观看网站| 最新国产乱人伦偷精品免费网站| 亚洲欧洲精品视频在线观看| 麻豆成人精品国产免费| 一级毛片免费观看不收费| 亚洲伦理一区二区| 免费视频中文字幕| 成全高清在线观看免费| 亚洲国产欧美日韩精品一区二区三区| 又爽又黄无遮挡高清免费视频| 国产真人无码作爱免费视频| 精品亚洲成A人无码成A在线观看| 亚洲 综合 国产 欧洲 丝袜| 美丽姑娘免费观看在线观看中文版| 国产成人精品日本亚洲专区6| 亚洲精品老司机在线观看|