- 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 中使用管道符號 (|) 來組合兩種或多種型別以實現聯合型別 -
Type1 | Type2 | Type3
聯合型別變數
我們可以定義一個不同型別聯合型別的變數。例如,
let value: number | string | boolean;
在上面的示例中,我們定義了一個名為 value 的聯合型別變數。我們可以為該變數分配數字、字串或布林值。
示例:聯合型別變數
在下面的示例中,變數的型別是聯合型別。這意味著變數可以包含數字或字串作為其值。
var val: string | number
val = 12
console.log("numeric value of val: "+val)
val = "This is a string"
console.log("string value of val: "+val)
編譯後,它將生成以下 JavaScript 程式碼。
var val;
val = 12;
console.log("numeric value of val: " + val);
val = "This is a string";
console.log("string value of val: " + val);
上述示例程式碼的輸出如下 -
numeric value of val: 12 string value of val: this is a string
聯合型別和函式引數
我們可以定義一個具有聯合型別引數的函式。當您可以呼叫該函式時,您可以傳遞聯合型別中任何型別的引數。例如,
function display(name: string | string[]){
// function body;
}
在上面的程式碼片段中,函式 display() 具有聯合型別(字串或字串陣列)的引數。您可以呼叫該函式,傳遞字串或字串陣列作為引數。
示例
在下面的示例中,我們定義了一個名為 disp 的函式,它具有聯合型別引數。
函式 disp() 可以接受字串或字串陣列型別的引數。
function disp(name: string|string[]) {
if(typeof name == "string") {
console.log(name)
} else {
var i;
for(i = 0;i<name.length;i++) {
console.log(name[i])
}
}
}
disp("mark")
console.log("Printing names array....")
disp(["Mark","Tom","Mary","John"])
編譯後,它將生成以下 JavaScript 程式碼。
function disp(name) {
if (typeof name == "string") {
console.log(name);
} else {
var i;
for (i = 0; i < name.length; i++) {
console.log(name[i]);
}
}
}
disp("mark");
console.log("Printing names array....");
disp(["Mark", "Tom", "Mary", "John"]);
輸出如下 -
Mark Printing names array…. Mark Tom Mary John
聯合型別和陣列
聯合型別也可以應用於陣列、屬性和介面。以下說明了聯合型別與陣列的用法。
示例
程式宣告一個數組。該陣列可以表示數字集合或字串集合。
var arr: number[]|string[];
var i: number;
arr = [1,2,4]
console.log("**numeric array**")
for(i = 0;i<arr.length;i++) {
console.log(arr[i])
}
arr = ["Mumbai","Pune","Delhi"]
console.log("**string array**")
for(i = 0;i<arr.length;i++) {
console.log(arr[i])
}
編譯後,它將生成以下 JavaScript 程式碼。
var arr;
var i;
arr = [1, 2, 4];
console.log("**numeric array**");
for (i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
arr = ["Mumbai", "Pune", "Delhi"];
console.log("**string array**");
for (i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
輸出如下 -
**numeric array** 1 2 4 **string array** Mumbai Pune Delhi
廣告