【问题标题】:Trying to create a declaration file for an existing API尝试为现有 API 创建声明文件
【发布时间】:2018-07-21 20:47:28
【问题描述】:

我正在尝试为 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();

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

EDIT 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;

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

导出默认api;

在打字稿代码中有

module.exports = api;

由转译过程生成

【问题讨论】:

  • 试试export default class ApiBuilderimport * as ApiBuilder from 'claudia-api-builder'
  • 很遗憾,您的建议如下:错误 TS2351:无法将“new”与类型缺少调用或构造签名的表达式一起使用。

标签: typescript


【解决方案1】:

好的,问题解决了。

首先,我在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;

感谢这个response SO。

为了完整起见,以下是我的 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;

【讨论】:

    猜你喜欢
    • 2017-10-13
    • 2021-02-04
    • 2016-07-07
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    相关资源
    最近更新 更多