/ typescript

ts使用js包,写类型声明文件

index.html

<script src="./greet/greet.js"></script>
<script src="./test.js"></script>

greet/greet.js

//命名空间
var Greeting;
(function (Greeting) {
Greeting.a = 1;
Greeting.b = 2;
Greeting.c = 3;
Greeting.z = 26;
Greeting.say = (function () {
function Greeting() {
}
Greeting.prototype.greet = function (name) {
console.log("Hello World!!!"+name);
};
return Greeting;
}());
})(Greeting || (Greeting = {}));

写test.ts文件

var obj = new Greeting.say(); 
obj.greet("Hello"); 
obj.greet(1); //这个地方就没有类型校验,因为是引用js文件,于是下面就需要些一个类型声明文件,让他支持这个类型检测

声明文件

// 创建一个类型声明文件 greet.d.ts

declare module Greeting { 
    export class say {
        greet(name:string) : string; 
    }
 }

修改 test.ts 文件

/// <reference path = "greet.d.ts" /> 
var obj = new Greeting.say(); 
obj.greet("Hello"); 
obj.greet(1); //这里就会有校验了