ts-基础数据类型

let isDone: boolean = false;
let decLiteral: number = 6;

let name: string = "bob";
name = "smith";

let list: number[] = [1, 2, 3];

// Declare a tuple type
let x: [string, number];
// Initialize it
x = ['hello', 10]; // OK

x[3] = 'world'; // OK, 字符串可以赋值给(string | number)类型
console.log(x[5].toString()); // OK, 'string' 和 'number' 都有 toString

enum Color {Red, Green, Blue};
let c: Color = Color.Green;

let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean

let notSure: any = 4;//Object 不行
notSure.ifItExists(); // okay, ifItExists might exist at runtime
notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)

let list: any[] = [1, true, "free"];
list[1] = 100;

function warnUser(): void {
    alert("This is my warning message");
}

// 声明一个void类型的变量没有什么大用,因为你只能为它赋予undefined和null:
// TypeScript里,undefined和null两者各自有自己的类型分别叫做undefined和null。 和void相似,它们的本身的类型用处不是很大:

let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;

// 我们使用let关键字来代替大家所熟悉的JavaScript关键字var。 let关键字是JavaScript的一个新概念,TypeScript实现了它。

 

posted @ 2017-08-31 10:37  alan-alan  阅读(190)  评论(0编辑  收藏  举报