1.基本数据类型
x
```interface ICar {
color:string;
}

class Bus implements ICar {
color:string;
constructor() {
this.color = "Blue";
}
}

var bus = new Bus();

//继承接口
interface Shape {
color: string;
}

interface PenStroke {
penWidth: number;
}

interface Square extends Shape, PenStroke {
sideLength: number;
}

interface ICar {
color:string;
safetyDevice?:any;//实现类无需实现
}

function MoveCar(car:ICar){
if(car.safetyDevice)
{
}
else
{
}
}```

2.类

x
```interface ICar {
color:string;
}

class Bus implements ICar {
color:string;
constructor() {
this.color = "Blue";
}
}

var bus = new Bus();

//继承接口
interface Shape {
color: string;
}

interface PenStroke {
penWidth: number;
}

interface Square extends Shape, PenStroke {
sideLength: number;
}

interface ICar {
color:string;
safetyDevice?:any;//实现类无需实现
}

function MoveCar(car:ICar){
if(car.safetyDevice)
{
}
else
{
}
}```

3.接口

```interface ICar {
color:string;
}

class Bus implements ICar {
color:string;
constructor() {
this.color = "Blue";
}
}

var bus = new Bus();

//继承接口
interface Shape {
color: string;
}

interface PenStroke {
penWidth: number;
}

interface Square extends Shape, PenStroke {
sideLength: number;
}

interface ICar {
color:string;
safetyDevice?:any;//实现类无需实现
}

function MoveCar(car:ICar){
if(car.safetyDevice)
{
}
else
{
}
}```

4.模块（Modules）

```module MyDemo {
export interface IDemo {

}

export class Demo implements IDemo {

}
}

module Shapes {
export module Polygons {
export class Triangle { }
export class Square { }
}
}

import polygons = Shapes.Polygons;
var sq = new polygons.Square(); // 类似于 'new Shapes.Polygons.Square()'```

5.函数（Function）

```function add(x:number, y:number):number {
return x + y;
}
// or
var myAdd = function (x:number, y:number):number {
return x + y;
};

function (x:number, y:number):number {
return x + y;
};

function (x:number, y:number):number {
return x + y;
};```

```var myAdd:(baseValue:number, increment:number)=>number =
function (x, y) {
return x + y;
};

//可选参数
function buildName(firstName:string, lastName?:string) {
if (lastName)
return firstName + " " + lastName;
else return firstName;
}
var result1 = buildName("Bob");

//默认参数
function buildNameDefaultValue(firstName: string, lastName = "Smith") {
return firstName + " " + lastName;
}
var result1 = buildNameDefaultValue("Bob");```

```function buildNameRest(firstName:string, ...restOfName:string[]) {
return firstName + " " + restOfName.join(" ");
}

var employeeName = buildNameRest("Joseph", "Samuel", "Lucas", "MacKinzie")
var people={
name:["张三","李四","王五","赵六"],
getName:function(){
return function(){
var i=Math.floor(Math.random()*4);
return {
n:this.name[i]
}
}
}
}

var pname=people.getName();

```var people = {
name: ["张三", "李四", "王五", "赵六"],
getName: function () {
return  ()=> {
var i = Math.floor(Math.random() * 4);
return {
n: this.name[i]
}
}
}
}

var pname = people.getName();

//重载
function student(name:string):string;
function student(age:number):number;
function student(numberorage:any):any {
if (numberorage && typeof (numberorage) == "string")
else
}
5.泛型

function identity<T>(arg: T): T {
return arg;
}

//数组泛型
function identity<T>(arg: T[]): T[] {
console.log(arg.length);
}

function identity<T>(arg:T):T {
return arg;
}
var myIdentity:<T>(arg:T)=>T = identity;//T也可使用其他字母表示
//也可以这么写
//var myIdentity:{<T>(arg:T): T} = identity;

interface GenericIdentityFn {
<T>(arg:T): T;
}

function identity<T>(arg:T):T {
return arg;
}

var myIdentity:GenericIdentityFn = identity;

class GenericNumber<T> {
zeroValue:T;
}

var myGenericNumber = new GenericNumber<number>();
myGenericNumber.zeroValue = 0;
myGenericNumber.add = function (x, y) {
return x + y;
};

interface Lengthwise {
length: number;
}

function loggingIdentity<T extends Lengthwise>(arg:T):T {
console.log(arg.length);
return arg;
}

loggingIdentity(3);//error
loggingIdentity({length: 10, value: 3});  //只要类型包含length属性即可

class Findable<T>
{
//...
}
function find<T>(n: T, s: Findable<T>) {
// ...
}```

6.合并

```interface Box {
height: number;
width: number;
}

interface Box {
scale: number;
}

var box: Box = {height: 5, width: 6, scale: 10};

module Animals {
exportclass Zebra { }
}

module Animals {
exportinterface Legged { numberOfLegs: number; }
exportclass Dog { }
}

//相当于
module Animals {
exportinterface Legged { numberOfLegs: number; }

exportclass Zebra { }
exportclass Dog { }
}```

```class Album {
label:Album.AlbumLabel;
}
module Album {
export class AlbumLabel {
}
}```

```function buildLabel(name:string):string {
return buildLabel.prefix + name + buildLabel.suffix;
}

module buildLabel {
export var suffix = "";
export var prefix = "Hello, ";
}

```enum Color {
red = 1,
green = 2,
blue = 4
}

module Color {
export function mixColor(colorName:string) {
if (colorName == "yellow") {
return Color.red + Color.green;
}
else if (colorName == "white") {
return Color.red + Color.green + Color.blue;
}
else if (colorName == "magenta") {
return Color.red + Color.blue;
}
else if (colorName == "cyan") {
return Color.green + Color.blue;
}
}
}```