TypeScript 增强了JavaScript面向对象的特性,使JavaScript的重构更加方便
学习文档 TypeScript(en-us) TypeScript(zh-cn)
数据类型 1 2 3 4 5 6 boolean number string <number >"1024" "1024" as number
声明变量
访问修饰符 1 2 3 4 public (default )private protected readonly
方法(函数) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 function Sum (a: number , b: number = 1 ): number { return a + b; } let Sub: (a: number , b?: number ) => number = function (a, b = 1 ): number { return a - b; } function People (a: string , ...b: string [] ): string { return `a : ${a} \rb : ${b} \rb.length : ${b.length} ` ; } function Avg (a: string ): string ;function Avg (a: number ): number ;function Avg (a: number , b: number ): number ;function Avg (x, y? ): any { if (typeof x === "number" ) { if (y === null ) { return x; } else { return (x + y) / 2 ; } } else { return "Error, can't get Avg from string" ; } };
类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 class Car { Name: string ; static Weight: number ; constructor ( ) { }; } class Bus extends Car { constructor ( ) { super (); } } abstract class Transportation { private _TrafficeName: string ; get TrafficeName(): string { return this ._TrafficeName; } set TrafficeName(trafficeName: string ) { this ._TrafficeName = trafficeName; } abstract Ticket(): number ; }
接口 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 interface IInterface { Name: string ; Address?: string ; } class ToImplements implements IInterface { Name: string ; Age: number ; } interface ToExtends extends IInterface { Sex: string ; } interface ToExtendsClass extends ToImplements { }
泛型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 function MyType <T >(theType: T ): T { return theType; } let output_a = MyType<string >("string" );let output_b = MyType("string" );function AnyType (theType: any ): any { return theType; }
枚举 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 enum Color { Blue, Black, Red = 6 , Yellow, Green } Color.Blue Color["Yellow" ] Color[6 ]
类型推论 类型兼容性 高级类型 Symbols 迭代器和生成器 1 2 3 4 5 6 7 8 9 10 11 let someArry = [1 , "string" , false ];for (let item of someArry) { console .log(item); } for (let item in someArry) { console .log(item); }
模块(外部模块) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 import { classA_1, classA_2 } from "ModuleA" ;class Class_A_1 implements classA_1 { _Name: string ; Sum(a: number , b: number ): number { return a + b; } } class Class_A_2 implements classA_2 {} import { classB_1, classB_2 } from "ModuleB" ;class Class_B_1 implements classB_1 {} class Class_B_2 implements classB_2 {} import {classA_1 as A1, classA_2 as A2, classB_1 as B1, classB_2 as B2 } from "AllModule" ;
命名空间(内部模块) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 namespace Animal { export class AnimalInfo { Name: string ; Voice: string ; LegNumber: number ; } export interface Behavior { Sounds(Info: AnimalInfo); } export class GetLegNumber { _TheAnimal: AnimalInfo; get TheAnimal(): AnimalInfo { return this ._TheAnimal; } set TheAnimal(newAnimalInfo: AnimalInfo) { this ._TheAnimal = newAnimalInfo; } constructor (Info: AnimalInfo ) { this .TheAnimal = Info; }; ReturnLegNumber(): number { return this .TheAnimal.LegNumber; } } } let info: Animal.AnimalInfo = new Animal.AnimalInfo();info.Name = "Duck" ; info.Voice = "Ga..Ga.." ; info.LegNumber = 2 ; class DuckBehavil implements Animal.Behavior { Sounds(Info: Animal.AnimalInfo) { console .log(Info.Voice); } } let duckBehvail: DuckBehavil = new DuckBehavil();duckBehvail.Sounds(info); import AInfo = Animal.AnimalInfo;let aInfo: AInfo = new AInfo();aInfo.Name = "Sheep" ; aInfo.Voice = "Mian..Mian.." ; aInfo.LegNumber = 4 ;
命名空间和模块 模块解析 声明合并 JSX 装饰器 Mixins 三斜线指令 Source code Practice
DefinitelyTyped 要在TypeScript中使用一些常用的JavaScript库,必须下载相应JavaScript库的TypeScript版本 (.d.ts),并使用三斜线指令引入