通过Nodejs模块编译Typescript
1.安装typescript模块
npm install typescript -g
2.对greeter.ts文件执行编译
tsc greeter.ts
就会得到javascript代码greeter.js
基础类型
//boolean
let isDone:boolean = false;
//number
let dec:number = 6;
//string
let aname:string = "bob";
console.log(aname);
aname = "aaa";
console.log(aname);
//模板字符串
let bname:string = "gene";
let age:number = 37;
let sentence:string = `hello,my name is ${bname}.i will be ${age+1} years old next month`;
console.log(sentence);
//array
let list:number[] = [1,2,3];
let list2:Array<number> = [1,2,3];
console.log(list);
console.log(list2);
//元组 tuple: 可以设定数组已知元素的类型,不同时则编译报错
let x :[string,number];
x = ["hello",10];
console.log(x);
//枚举
enum Color{Red,Green,Blue};
let c:Color = Color.Blue;
console.log(c);
//任意类型
let notsure:any = 4;
notsure = "maybe";
notsure = false;
console.log(notsure);