尝试为现有API

时间:2018-02-11 16:25:15

标签: typescript

我正在尝试为existing nmp module创建定义文件。

基本上,这个模块导出一个主要功能:

module.exports = function ApiBuilder(options) {...}

以简单的形式,它使用如下:

var ApiBuilder = require('claudia-api-builder'),
  api = new ApiBuilder();

module.exports = api;

api.get('/hello', function () {
  return 'hello world';
});

我已经创建了一个简单的定义文件,如下所示:

declare module "claudia-api-builder" {

    export class ApiBuilder {
        constructor(options?: any);

        get(uri: string, callback: Function): void;

    }
}

以下来源.ts代码:

import { ApiBuilder } from 'claudia-api-builder';

const api = new ApiBuilder();

api.get('/hello', function () {
    return 'hello world';
});

export default api;

转换为以下javascript(es2016):

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const claudia_api_builder_1 = require("claudia-api-builder");
const api = new claudia_api_builder_1.ApiBuilder();
api.get('/hello', function () {
    return 'hello world';
});
exports.default = api;

上述代码失败,出现以下错误:

  

claudia_api_builder_1.ApiBuilder不是构造函数

当然,已编译的代码应该是:

const api = new claudia_api_builder_1();

显然,我在这里忽略了这一点。 任何帮助表示赞赏。

编辑1 :解决了以下index.d.ts文件的问题:

declare module "claudia-api-builder" {

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

    /*~ Write your module's methods and properties in this class */
    class ApiBuilder {
        constructor(options?: any);

        get(uri: string, callback: Function): void;
    }

}

现在,转化过程让我:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ApiBuilder = require("claudia-api-builder");
const api = new ApiBuilder();
api.get('/hello', function () {
    return 'hello world';
});
exports.default = api;

这里剩下的问题是我需要替换:

  

export default api;

在打字稿代码中

  

module.exports = api;

由转换过程生成

1 个答案:

答案 0 :(得分:0)

好的,问题解决了。

首先,我在Typescript documentation找到了我需要的东西。 这有助于我选择模板开始:

  

如果您的模块可以使用new构建,请使用module-class.d.ts

声明文件现在是:

declare module "claudia-api-builder" {

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

    /*~ Write your module's methods and properties in this class */
    class ApiBuilder {
        constructor(options?: any);

        get(uri: string, callback: Function): void;
    }

}

ts源文件已成为:

import ApiBuilder = require('claudia-api-builder');

const api = new ApiBuilder();

api.get('/hello', function () {
    return 'hello world';
});

export = api;

感谢SO {/ 3>上的response

为了完整性,以下是我的tsconfig.json:

    {
      "compilerOptions": {
        /* Basic Options */
        "target": "es2016", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
        "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    output to single file. */
        "outDir": "./dist/", /* Redirect output structure to the directory. */
        "rootDir": "./src/", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
/* Strict Type-Checking Options */
    "strict": true, /* Enable all strict type-checking options. */
        /* Module Resolution Options */
        "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
        "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
      }
    }

最后,转换成代码:

"use strict";
const ApiBuilder = require("claudia-api-builder");
const api = new ApiBuilder();
api.get('/hello', function () {
    return 'hello world';
});
module.exports = api;