
- TypeScript 基礎
- TypeScript - 首頁
- TypeScript - 路線圖
- TypeScript - 概述
- TypeScript - 環境設定
- TypeScript - 基本語法
- TypeScript vs. JavaScript
- TypeScript - 特性
- TypeScript - 變數
- TypeScript - let & const
- TypeScript - 運算子
- TypeScript 基本型別
- TypeScript - 型別
- TypeScript - 型別註解
- TypeScript - 型別推斷
- TypeScript - 數字
- TypeScript - 字串
- TypeScript - 布林值
- TypeScript - 陣列
- TypeScript - 元組
- TypeScript - 列舉
- TypeScript - Any
- TypeScript - Never
- TypeScript - 聯合型別
- TypeScript - 字面量型別
- TypeScript - 符號
- TypeScript - null vs. undefined
- TypeScript - 類型別名
- TypeScript 控制流
- TypeScript - 決策
- TypeScript - If 語句
- TypeScript - If Else 語句
- TypeScript - 巢狀 If 語句
- TypeScript - Switch 語句
- TypeScript - 迴圈
- TypeScript - For 迴圈
- TypeScript - While 迴圈
- TypeScript - Do While 迴圈
- TypeScript 函式
- TypeScript - 函式
- TypeScript - 函式型別
- TypeScript - 可選引數
- TypeScript - 預設引數
- TypeScript - 匿名函式
- TypeScript - 函式構造器
- TypeScript - Rest 引數
- TypeScript - 引數解構
- TypeScript - 箭頭函式
- TypeScript 介面
- TypeScript - 介面
- TypeScript - 擴充套件介面
- TypeScript 類和物件
- TypeScript - 類
- TypeScript - 物件
- TypeScript - 訪問修飾符
- TypeScript - 只讀屬性
- TypeScript - 繼承
- TypeScript - 靜態方法和屬性
- TypeScript - 抽象類
- TypeScript - 訪問器
- TypeScript - 鴨子型別
- TypeScript 高階型別
- TypeScript - 交叉型別
- TypeScript - 型別守衛
- TypeScript - 型別斷言
- TypeScript 型別操作
- TypeScript - 從型別建立型別
- TypeScript - Keyof 型別運算子
- TypeScript - Typeof 型別運算子
- TypeScript - 索引訪問型別
- TypeScript - 條件型別
- TypeScript - 對映型別
- TypeScript - 模板字面量型別
- TypeScript 泛型
- TypeScript - 泛型
- TypeScript - 泛型約束
- TypeScript - 泛型介面
- TypeScript - 泛型類
- TypeScript 雜項
- TypeScript - 三斜槓指令
- TypeScript - 名稱空間
- TypeScript - 模組
- TypeScript - 環境宣告
- TypeScript - 裝飾器
- TypeScript - 型別相容性
- TypeScript - Date 物件
- TypeScript - 迭代器和生成器
- TypeScript - Mixins
- TypeScript - 實用程式型別
- TypeScript - 裝箱和拆箱
- TypeScript - tsconfig.json
- 從 JavaScript 到 TypeScript
- TypeScript 有用資源
- TypeScript - 快速指南
- TypeScript - 有用資源
- TypeScript - 討論
TypeScript - 泛型介面
泛型介面
在 TypeScript 中,泛型介面類似於介面,但它使用一個或多個型別引數進行定義。這些型別引數允許介面與不同的資料型別一起重用,同時仍然強制執行型別安全性和一致性。
TypeScript 中的介面是一種定義物件或類結構的方法。它指定了物件或類應該具有的預期格式,從而可以確保程式碼中的一致性和型別安全性。
語法
您可以按照以下語法定義泛型介面。
interface IGeneric<T> { value1: T; value2: T; }
在上述語法中,'IGeneric' 是一個型別化介面,它接受自定義資料型別。在介面中,我們使用型別 'T' 作為屬性 value1 和 value2 的值。
示例
在下面的程式碼中,我們建立了帶有自定義型別 'T' 的 'IGeneric' 介面。它具有型別為 'T' 的 value1 和 value2 屬性。
接下來,我們定義了 IGeneric 介面型別的 'obj' 物件,並使用數字資料型別作為介面的泛型型別。之後,我們在輸出中列印物件的 'value1' 屬性的值。
// Generic interface interface IGeneric<T> { value1: T; value2: T; } // Object of generic interface let obj: IGeneric<number> = { value1: 10, value2: 20 }; console.log(obj.value1);
編譯後,它將生成以下 JavaScript 程式碼
// Object of generic interface let obj = { value1: 10, value2: 20 }; console.log(obj.value1);
輸出
上述示例程式碼的輸出如下:
10
示例
在此程式碼中,我們使用 'T' 和 'U' 兩種資料型別與 'IGeneric' 介面。介面中 value1 屬性的型別為 'T',value2 屬性的型別為 'U'。
接下來,我們定義了具有數字和字串自定義資料型別的介面型別的 'obj' 物件。
// Generic interface interface IGeneric<T, U> { value1: T; value2: U; } // Define object with a generic interface let obj: IGeneric<number, string> = { value1: 10, value2: "Hello" }; console.log(obj.value2);
編譯後,它將生成以下 JavaScript 程式碼
// Define object with a generic interface let obj = { value1: 10, value2: "Hello" }; console.log(obj.value2);
輸出
上述示例程式碼的輸出如下:
Hello
示例
下面的示例與前一個示例非常相似,但 'IGeneric' 介面包含 merge() 方法,該方法採用型別為 'U' 的引數並返回型別為 'U' 的值。
在定義 'obj' 物件時,我們使用了數字和字串資料型別與介面。這意味著 merge() 方法將採用兩個字串引數,並返回一個字串值。
在輸出中,程式碼列印了連線的字串,我們將其作為 merge() 方法的引數傳遞。
// Generic interface interface IGeneric<T, U> { value1: T; // method that returns the value of type U merge: (a: U, b: U) => U; } // Define object with a generic interface let obj: IGeneric<number, string> = { value1: 10, merge: (a, b) => a + b }; console.log(obj.merge("Hello", "world!")); // Hello world!
編譯後,它將生成以下 JavaScript 程式碼
// Define object with a generic interface let obj = { value1: 10, merge: (a, b) => a + b }; console.log(obj.merge("Hello", "world!")); // Hello world!
輸出
Helloworld!
泛型介面作為函式型別
開發人員還可以將泛型介面用作函式型別。這使您能夠在各種型別和場景中使用相同的函式介面,從而確保型別安全,而不會犧牲靈活性。
示例
在下面的程式碼中,'func_interface' 接受泛型型別 T 和 U。它定義了函式的結構,該函式採用型別為 'T' 的引數並返回型別為 'U' 的值。
接下來,我們定義了返回字串長度的函式表示式,並將其儲存在 'stringToLength' 變數中。函式表示式的型別使用帶有字串和數字資料型別的泛型介面定義。
類似地,我們定義了另一個將數字轉換為字串的函式,其型別為帶有字串和數字資料型別的 func_interface。
接下來,我們呼叫這些函式並在控制檯中列印它們的輸出。
// Define a generic interface for a function interface func_interface<T, U> { (input: T): U; } // A specific function that matches the func_interface interface const stringToLength: func_interface<string, number> = (input) => { return input.length; }; // Using the function const result = stringToLength("Hello, TypeScript!"); // returns 18 console.log(result); // Another function that matches the func_interface interface const numberToString: func_interface<number, string> = (input) => { return `Number: ${input}`; }; // Using the second function const output = numberToString(123); // returns "Number: 123" console.log(output);
編譯後,它將生成以下 JavaScript 程式碼
// A specific function that matches the func_interface interface const stringToLength = (input) => { return input.length; }; // Using the function const result = stringToLength("Hello, TypeScript!"); // returns 18 console.log(result); // Another function that matches the func_interface interface const numberToString = (input) => { return `Number: ${input}`; }; // Using the second function const output = numberToString(123); // returns "Number: 123" console.log(output);
輸出
18 Number: 123
簡而言之,泛型介面允許開發人員使用多個數據型別重用介面。泛型介面可以用作物件或函式型別,允許使用不同的結構重用單個函式或物件。