TypeScript 基本类型
🏷️ TypeScript
TS 代码
js
// 基本类型
// 布尔类型
let isDone: boolean = false;
// 数值类型
let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;
// 字符类型
let color: string = "blue";
name = 'red';
// 多行文本
let fullName: string = `Bob Bobbington`;
let age: number = 37;
let sentence: string = `Hello, my name is ${ name }.
I'll be ${ age + 1 } years old next month.`
// 数组类型
let list: number = [1, 2, 3];
let lista: Array<number> = [1, 2, 3];
// 元组类型
// Declare a tuple type
let x: [string, number];
// Initialize it
x = ['hello', 10]; // OK
// Initialize it incorrectly
// x = [10, 'hello']; // Error
console.log(x[0].substr(1)); // OK
// console.log(x[1].substr(1)); // Error, 'number' does not have 'substr'
x[3] = 'world'; // OK, string can be assigned to (string | number)
console.log(x[3].toString()); // OK, 'string' and 'number' both have toString
//x[6] = true; // Error, boolean isn't (string | number)
// 枚举
enum Color {Red, Green, Blue};
let c: Color = Color.Green;
// 可以自定义枚举项的开始值
enum ColorA {Red = 1, Green, Blue};
let ca: ColorA = ColorA.Green;
// 也可以自定义所有枚举项的值
enum ColorB {Red = 1, Green = 2, Blue = 4};
let cb: ColorB = ColorB.Green;
// 可以根据值 map 到对应的名字
enum ColorC {Red = 1, Green, Blue};
let colorName: string = ColorC[2];
console.log(colorName);
// Any 类型
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean
//
let notSureA: any = 4;
notSureA.ifItExists(); // okay, ifItExists might exist at runtime
notSureA.toFixed(); // okay, toFixed exists (but the compiler doesn't check)
let prettySure: Object = 4;
// prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.
// 可能包含多种类型的数组
let listAny: any = [1, true, "free"];
listAny[1] = 100;
// void 与 any 相反,没有任何类型
function warnUser(): void {
alert("This is my warning message");
}
// void 类型只能设置为 undefined 或者 null
let unusable: void = undefined;
// 类型断言 (尖括号)
let someValue: any = "this is a string";
let strLength: number = (<string>someValue).length;
// 类型断言 (as)
let someValue2: any = "this is a string";
let strLength2: number = (someValue2 as string).length;
// 关于 let
// 使用 let 代替 var 创建变量,可以减少一些 js 中问题
编译生成的 JS 代码
js
// 基本类型
// 布尔类型
var isDone = false;
// 数值类型
var decimal = 6;
var hex = 0xf00d;
var binary = 10;
var octal = 484;
// 字符类型
var color = "blue";
name = 'red';
// 多行文本
var fullName = "Bob Bobbington";
var age = 37;
var sentence = "Hello, my name is " + name + ".\n\nI'll be " + (age + 1) + " years old next month.";
// 数组类型
var list = [1, 2, 3];
var lista = [1, 2, 3];
// 元组类型
// Declare a tuple type
var x;
// Initialize it
x = ['hello', 10]; // OK
// Initialize it incorrectly
// x = [10, 'hello']; // Error
console.log(x[0].substr(1)); // OK
// console.log(x[1].substr(1)); // Error, 'number' does not have 'substr'
x[3] = 'world'; // OK, string can be assigned to (string | number)
console.log(x[3].toString()); // OK, 'string' and 'number' both have toString
//x[6] = true; // Error, boolean isn't (string | number)
// 枚举
var Color;
(function (Color) {
Color[Color["Red"] = 0] = "Red";
Color[Color["Green"] = 1] = "Green";
Color[Color["Blue"] = 2] = "Blue";
})(Color || (Color = {}));
;
var c = Color.Green;
// 可以自定义枚举项的开始值
var ColorA;
(function (ColorA) {
ColorA[ColorA["Red"] = 1] = "Red";
ColorA[ColorA["Green"] = 2] = "Green";
ColorA[ColorA["Blue"] = 3] = "Blue";
})(ColorA || (ColorA = {}));
;
var ca = ColorA.Green;
// 也可以自定义所有枚举项的值
var ColorB;
(function (ColorB) {
ColorB[ColorB["Red"] = 1] = "Red";
ColorB[ColorB["Green"] = 2] = "Green";
ColorB[ColorB["Blue"] = 4] = "Blue";
})(ColorB || (ColorB = {}));
;
var cb = ColorB.Green;
// 可以根据值 map 到对应的名字
var ColorC;
(function (ColorC) {
ColorC[ColorC["Red"] = 1] = "Red";
ColorC[ColorC["Green"] = 2] = "Green";
ColorC[ColorC["Blue"] = 3] = "Blue";
})(ColorC || (ColorC = {}));
;
var colorName = ColorC[2];
console.log(colorName);
// Any 类型
var notSure = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean
//
var notSureA = 4;
notSureA.ifItExists(); // okay, ifItExists might exist at runtime
notSureA.toFixed(); // okay, toFixed exists (but the compiler doesn't check)
var prettySure = 4;
// prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.
// 可能包含多种类型的数组
var listAny = [1, true, "free"];
listAny[1] = 100;
// void 与 any 相反,没有任何类型
function warnUser() {
alert("This is my warning message");
}
// void 类型只能设置为 undefined 或者 null
var unusable = undefined;
// 类型断言 (尖括号)
var someValue = "this is a string";
var strLength = someValue.length;
// 类型断言 (as)
var someValue2 = "this is a string";
var strLength2 = someValue2.length;
// 关于 let
// 使用 let 代替 var 创建变量,可以减少一些 js 中问题
//# sourceMappingURL=basictype.js.map