Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript学习笔记(一) #11

Open
ychow opened this issue Apr 14, 2015 · 0 comments
Open

TypeScript学习笔记(一) #11

ychow opened this issue Apr 14, 2015 · 0 comments

Comments

@ychow
Copy link
Owner

ychow commented Apr 14, 2015

基本类型

布尔值

var isDone: boolean = false;

数字类型

和JavaScript一样,TypeScript中的numbers都是浮点类型的。

var height: number = 6;

字符串

在 TypeScript中,也使用双引号或者单引号来包着字符串。

var name: string = "bob";
name = 'smith';

数组

在TypeScript中,你有两种方法来创建一个数组:

  1. 第一种,你可以先写上元素的类型,然后跟上 '[]' ,这种创建出来的数组元素类型只能是单一的。
var list:number[] = [1, 2, 3];

2.第二种就是常见的一种, Array<元素类型>。

var list:Array<number> = [1, 2, 3];

枚举

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

泛类型(Any)

在最开始当我们创建一个变量时,我们不能明确知道它会是某一种类型,这时候我们就可以使用 Any 。

var notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; 

而且我们还可以创建一个包含着多种数据类型的数组

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

list[1] = 100;

空(Void)

当你想让方法不能返回任何值的时候,你就可以使用 Void。

function warnUser(): void {
    alert("This is my warning message");  /*alert是可以执行的,只有return不能返回任何值*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant