简体   繁体   中英

How to Setup VS Code For Creating TypeScript Definition Files

I have this current folder structure:

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

where I have a JS file that I'm trying to make a Typescript Definition (d.ts) for.

The js file looks like this:

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

The index.d.ts file looks like this:

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

And in my test file I'm attempting to write this:

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

But I get an error on the constructor Cannot find name FileToMakeTdFor

How do I make the test ts file find the index.d.ts file? Is there a better way to setup this structure?

Special thanks to Dave Hillier's link . It lead me to the TypeScript Handbook for declaration files . Apparently I was just missing some export calls:

/*~ 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;
}

Once I added the export as namespace FileToMakeTdFor; and export = FileToMakeTdFor; lines, everything started working...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM