前端之家收集整理的这篇文章主要介绍了
typescript数据类型,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
// 布尔类型
let isDone: boolean = false;
// 数字类型 所有数字都是浮点数 number
let decLiteral: number = 6;
let hexLiteral: number = 0xf00d;
let binaryLiteral: number = 0b1010;
let octalLiteral: number = 0o744;
// 字符串
let username: string = "bob";
username = "smith";
// 可以使用模版字符 定义多行文本和内嵌表达式
let uname: string = "gene";
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 list1: Array<number> = [1,3];
// 元组
let x: [string,number];
x = ["hello",10];
console.log(x[0].substr(1));
//枚举
enum Color {
Red,Green,Blue
}
let c: Color = Color.Green;
//任意值
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false;
notSure.ifItExists();
notSure.toFixed();
// 空值 没有任何类型
function warnUser(): void {
alert("This is my warning message");
}
let unusable: void = undefined;
// null undefined
let u: undefined = undefined;
let n: null = null;
// Never 永不存在的值类型
function error(message: string): never {
throw new Error(message);
}
function fail() {
return console.error("something Failed ");
}
//Object 表示非原始类型 除 number,sting,boolean,symbol,null underfined之外的类型
declare function create(o: object|null):void;
create({prop:0});
create(null);
原文链接:/typescript/845631.html