TypeScript中正確使用interface和type的方法實例
前言
interface 和 type 都是用來定義類型,可以理解定義 shape ,那么 shape 表示一種設計大框,或者說只要具有某些特征或者行為就是為一類事物。在有些面向例如 Java 語言中,interface 用于定義行為,如果一個類實現(xiàn)了某一個 interface 表示該類具有某種行為或者說具有某種能力,例如writable 或者 readable ??梢酝ㄟ^行為來對事物進行劃分。interface 在 golang 語言中應用風生水起,但是在 TypeScript interface 更偏于一種類型 shape,同時 TypeScript 也提供了 type 用于定義類型,其實 interface 和 type 在 TypeScript 中區(qū)別不大,但是還是有點區(qū)別。
interface
interface 可以用于對類(class)、對象(object)或者函數(shù)(function)進行 shape。
interface Tut{ title:string isComplete:boolean }
定義了一個接口 interface 用于表示 Tut 類型, 然后定義類型 Tut 的變量 machineLearningTut
const machineLearningTut:Tut = { title:"machine learning basic", isComplete:true }
如果類型為 Tut 的 machineLearningTut 沒有完全實現(xiàn)接口接口定義屬性或者方法就會在編譯階段給出友好的提示
const machineLearningTut:Tut = { title:"machine learning basic", }
提示類型 Tut 的 machineLearningTut 沒有實現(xiàn) isComplete 這個在接口中已經(jīng)聲明的屬性。
Property 'isComplete' is missing in type '{ title: string; }' but required in type 'Tut'.ts(2741) [demo2.ts(3, 5): ]()'isComplete' is declared here.
class VideoTut implements Tut{ title:string; isComplete:boolean; }
也可以定義類 VideoTut 實現(xiàn) Tut 接口
interface Greet{ (name:string):string } const greet:Greet = (name)=> `hello ${name}`
也可以定義 Greet 接口用于 shape 函數(shù),定義函數(shù)的參數(shù)和函數(shù)返回值類型
interface AdvanceTut extends Tut{ isFree:boolean } const machineLearningTut:AdvanceTut = { title:"machine learning basic", isComplete:true, isFree:true }
接口間是可以通過 extend 來實現(xiàn)接口間的繼承(擴展),AdvanceTut 是對 Tut 的擴展,在 type 不支持 extend 來實現(xiàn) type 間的擴展。
interface Tut{ title:string isComplete:boolean } interface Tut{ isFree:boolean } const machineLearningTut:Tut = { title:"machine learning basic", isComplete:true, isFree:true }
上面連續(xù)聲明了兩個 Tut 同名 inteface 這兩 Tut 顯示是擴展的關系,并不是覆蓋的關系,這一點也是 type 也是不具備的特點
type
其實 type 用法和 interface 用法大同小異,不過 type 便于類型,可以是 JavaScript 基礎類型的別名。其實 type 從本質(zhì)還是和 interface 還是有些區(qū)別,這個可能即使解釋了還需要大家在實踐過程慢慢體會。
type isComplete = boolean type title = string type greet = (name:string)=>string type Tut = { title:string; isComplete:boolean } const machineLearningTut:Tut = { title:"machine learning title", isComplete:true }
type Tut = { title:string; isComplete:boolean } & { isFree:boolean } const machineLearningTut:Tut = { title:"machine learning title", isComplete:true, isFree:true }
type 類型可以 & 實現(xiàn)對 type 的擴展
type VideoTut = Tut | { isFree:boolean } const machineLearningTut:VideoTut = { title:"machine learning title", isComplete:true, isFree:true }
export type InputProps = { type:'text'|'email'; value:string; onChane:(newValue:string)=>void }
而且前后端定義類型也可以用 type 來實現(xiàn),如下可以定義多個基本類型,這些定義好的類型可以定義新的類型。
type onChaneType = (newValue:string)=>void type InputType = 'text'|'email'; type InputValue = string export type InputProps = { type:InputType; value:InputValue; onChane:onChaneType }
附:interface和type不同點
type可以聲明基本類型別名、聯(lián)合類型、元祖等類型
// 基本類型別名 type Name = string; // 聯(lián)合類型 interface Dog { wong() } interface Cat { miao(); } type Pet = Dog | Cat; // 具體定義數(shù)組每個位置的類型 type PetList = [Dog, Pet];
type語句中還可以使用typeof獲取實例的類型進行賦值
// 當你想要獲取一個變量的類型時,使用typeof let div = document.createElement('div'); type B = typeof div;
type其他騷操作
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能夠聲明合并
interface User { name: string; age: number; } interface User { sex: string; }
User接口為:
{ name: string; age: number; sex: string; }
總結
到此這篇關于TypeScript中正確使用interface和type的文章就介紹到這了,更多相關TypeScript使用interface和type內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
uni-app開發(fā)微信小程序遇到的部分踩坑實戰(zhàn)
最近在用uni-app開發(fā)微信小程序,這里將開發(fā)中遇到的坑和問題記錄一下,所以下面這篇文章主要給大家介紹了關于uni-app開發(fā)微信小程序遇到的部分踩坑,需要的朋友可以參考下2023-02-02js 實現(xiàn)圖片預加載(js操作 Image對象屬性complete ,事件onload 異步加載圖片)
通過js操縱DOM很多情況下都是為了實現(xiàn)和當前頁html元素的異步載入,我談談對Image對象的一些認識。2011-03-03TypeScript中的接口Interface詳解(對象類型的強大工具)
TypeScript中的接口是一個強大而靈活的特性,它為我們提供了一種清晰、簡潔的方式來定義對象的結構和類型,通過使用接口,我們可以編寫更加健壯、可維護的代碼,這篇文章主要介紹了TypeScript中的接口(Interface):對象類型的強大工具,需要的朋友可以參考下2024-08-08全面解析Bootstrap中tooltip、popover的使用方法
這篇文章主要為大家詳細解析了Bootstrap中tooltip、popover的使用方法,了解提示框、彈出框的實現(xiàn)原理,感興趣的朋友可以參考一下2016-06-06