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, 2, 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);

  

posted @ 2018-08-06 11:07  smodi  阅读(404)  评论(0编辑  收藏  举报