=========【网址】=========
1、
http://www.tslang.cn/
2、编译选项
http://www.tslang.cn/docs/handbook/compiler-options.html
3、
https://www.gitbook.com/book/zhongsp/typescript-handbook/details
=========【安装TypeScript】=========
有两种主要的方式获取TypeScript工具。
1、安装方式一
通过npm(Node.js包管理器)
安装好Node.js后,打开cmd窗口,输入以下命令
npm install -g typescript
2、安装方式二
安装TypeScript的Visual Studio插件
Visual Studio2015和Visual Studio 2013 Update 2默认包含了TypeScript。如果你没有安装包含TypeScript的Visual Studio ,你仍然可以下载。
=========【常用命令】=========
1、安装
npm install -g typescript
2、查看版本
tsc -v 或 tsc --version
3、更新版本
npm update -g typescript
4、编译代码
tsc helloworld.ts
tsc **********.ts (编译 **********.ts 文件)
5、帮助
tsc -h 或 tsc --help
6、监视模式下运行编译器。会监视输出文件,在它们改变时重新编译:
tsc --watch 或 tsc -w
=========【基础】=========
*、内置类型
var name : string = 'Felia' 【字符串:string】
var age : number = 36 【数字:number】
var married : boolean = true 【布尔类型:boolean】
var jobs : Array<string> = ['IBM' , 'Microsoft' , 'Google']; 【数组:Array】
var jobs : string[] = ['IBM' , 'Microsoft' , 'Google']; 【数组:Array】
【枚举:enum】
enum Role {Employee, Manager, Admin};
var role : Role = Role.Employee;
//Enum
enum Color {Red, Green, Blue}
var c: Color = Color.Red;
alert(c);//默认值从0开始,alert(0);
//可以手动指定值
enum Color1 {Red = 1, Green, Blue}
var c1: Color1 = Color1.Green;
alert(c1);//alert(2)
//根据值查找名称
enum Color2 {Red = 1, Green=2, Blue=4}
var c2: string = Color2[4];
alert(c2);//alert(Blue)
【any类型】
var something : any = 'as string';
something = 1;
something = [1,2,3];
*、let
let user = "Jane User";
let user = [0, 1, 2];
let user = { firstName: "Jane", lastName: "User" };
let user = new Student("Jane", "M.", "User");
*、函数
function greeter(person: string) { // 字符串类型
return "Hello, " + person;
}
function greeter(person: Person) { // 接口对象
return "Hello, " + person.firstName + " " + person.lastName;
}
*、接口
interface Person {
firstName: string;
lastName: string;
}
*、类
class Student {
fullName: string;
age: number;
constructor(public firstName, public middleInitial, public lastName) {
this.fullName = firstName + " " + middleInitial + " " + lastName;
}
greet() {
console.log("Hello", this.fullName);
}
ageInYears(years : number) : number {
return this.age + years;
}
}
*、构造函数(constructor)
# 构造函数是当类进行实例化时执行的特殊函数,必须命名为constructor;
# 在Typescript中,每个类只能有一个构造函数。
*、方法
*、继承(extends)
用extends关键字实现
class Animal {
animalName:string;
constructor(name:string) {
this.animalName = name;
}
sayHello() {
alert(this.animalName + ": Hello");
}
}
class Cat extends Animal {
//重写sayHello方法
sayHello() {
alert(this.animalName + "(Cat):" + "Hello");
}
}
*、修饰符、get,set 访问器、静态属性
class Animal {
private animalName:string;//默认是public
static width = 100;
static height = 200;
private _animalName:string;//默认是public
get animalName():string {
return this._animalName;
}
set animalName(name:string):string {
this._animalName = name;
}
}
*、工具
箭头函数“(=>)”
示例:
var data: string[] = ['Alice Green', 'Paul Pfifer', 'Louis Blakenship'];
data.forEach( (line) => console.log(line) );
当只有一个参数时,圆括号可以省略。箭头(=>)语法可以用作表达式:
示例:
var evens = [2,4,6,8];
var odds = evens.map(v => v + 1);
也可以用作语句:
data.forEach( line => {
console.log(line.toUpperCase())
});