如何设置VS代码以创建TypeScript定义文件

时间:2017-01-06 14:53:47

标签: javascript typescript visual-studio-code

我有这个当前的文件夹结构:

project
└───typings
│   |   index.d.ts
|   FileToMakeTdFor.js
|   FileToMakeTdFor-Tests.ts

我有一个JS文件,我试图为其制作一个Typescript Definition(d.ts)。

js文件如下所示:

"use strict";
var FileToMakeTdFor = (function () {
    function FileToMakeTdFor(config) {
        ...
    }
    FileToMakeTdFor.prototype.example = function () {
        ...
    };
    return CRMWebAPI;
}());
exports.FileToMakeTdFor = FileToMakeTdFor;

index.d.ts文件如下所示:

export interface FileToMakeTdFor {
    new (config:any): FileToMakeTdFor;
    example(): void;
}

在我的测试文件中,我试图写这个:

let obj = new FileToMakeTdFor();
obj.example();

但是我在构造函数Cannot find name FileToMakeTdFor

上收到错误

如何让测试ts文件找到index.d.ts文件?有没有更好的方法来设置这个结构?

1 个答案:

答案 0 :(得分:1)

特别感谢Dave Hillier的link。它引导我进入TypeScript Handbook for declaration files。显然我只是错过了一些出口电话:

/*~ If this module is a UMD module that exposes a global variable 'CRMWebAPI' when
 *~ loaded outside a module loader environment, declare that global here.
 *~ Otherwise, delete this declaration.
 */
export as namespace FileToMakeTdFor;

/*~ This declaration specifies that the class constructor function
 *~ is the exported object from the file
 */
export = FileToMakeTdFor;

export interface FileToMakeTdFor {
    new (config:any): FileToMakeTdFor;
    example(): void;
}

一旦我添加了export as namespace FileToMakeTdFor;export = FileToMakeTdFor;行,一切都开始工作......