TypeScript的基本数据类型
原创约 408 字大约 1 分钟...
TypeScript的基本数据类型
注意
本博文仅供学术研究和交流参考,严禁将其用于商业用途。如因违规使用产生的任何法律问题,使用者需自行负责。
Boolean
Number
String
Array
Enum //枚举
Any //不知道数据类型
Void //声明函数 可以不需要返回值

let isDone: boolean = false;
Number
let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;
String
let color: string = "blue";
color = 'red';
Array
let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3];
Tuple
// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10]; // OK
// Initialize it incorrectly
x = [10, "hello"]; // Error
Enum
enum Color {Red, Green, Blue}
let c: Color = Color.Green;
enum Color {Red = 1, Green, Blue}
let c: Color = Color.Green;
enum Color {Red = 1, Green = 2, Blue = 4}
let c: Color = Color.Green;
enum Color {Red = 1, Green, Blue}
let colorName: string = Color[2];
alert(colorName); // Displays 'Green' as its value is 2 above
Any
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean
let notSure: any = 4;
notSure.ifItExists(); // okay, ifItExists might exist at runtime
notSure.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 list: any[] = [1, true, "free"];
list[1] = 100;
Void
function warnUser(): void {
alert("This is my warning message");
}
let unusable: void = undefined;
Null and Undefined
// Not much else we can assign to these variables!
let u: undefined = undefined;
let n: null = null;
分割线
相关信息
以上就是我关于 TypeScript的基本数据类型 知识点的整理与总结的全部内容,希望对你有帮助。。。。。。。
扩展知识
Powered by Waline v2.15.4