TypeScript 型別
Boolean
true or false
使用方式
var isSuccess: boolean = true;
if (isSuccess) {
console.log("Success!");
}
Number
所有數值都是浮點數
使用方式
var total: number = 0;
var num1: number = 1;
var num2: number = 3.1;
total = num1 + num2;
console.log(total);
String
使用方式
var title: string = "今日頭條新聞";
Template Literals 使用方式
var userName = "Allen";
var msg = `Hello, ${this.userName}`;
console.log(msg);
Array
存放一組資料,與Javascript操作相同。
使用方式
var idList: number[] = [1, 2, 3];
var nameList: string[] = ["Allen", "Daniel", "James"];
for (var item of idList) {
console.log(item);
}
for (var userName of nameList) {
console.log(userName);
}
Enum
使用方式
enum Size {
Large = 0,
Medium = 1,
Small = 2
}
var size = Size.Large;
console.log(size); //0
console.log(Size[0]); //Large
Any
等同於Javascript的變數 不建議使用
使用方式
var data: any = 1;
data = "Allen";
data = ["Allen", "Daniel"];
console.log(data[0]);