1

我正在尝试.d.ts为此灯塔文件创建一个,并且努力了解如何处理默认导出

https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/index.js#L28

module.exports = function(url, flags = {}, configJSON) {
  const startTime = Date.now();
  return Promise.resolve().then(_ => {
    // set logging preferences, assume quiet
    flags.logLevel = flags.logLevel || 'error';
    log.setLevel(flags.logLevel);

    // Use ConfigParser to generate a valid config file
    const config = new Config(configJSON, flags.configPath);

    const connection = new ChromeProtocol(flags.port, flags.hostname);

    // kick off a lighthouse run
    return Runner.run(connection, {url, flags, config})
      .then(lighthouseResults => {
        // Annotate with time to run lighthouse.
        const endTime = Date.now();
        lighthouseResults.timing = lighthouseResults.timing || {};
        lighthouseResults.timing.total = endTime - startTime;

        return lighthouseResults;
      });
  });
};

module.exports.getAuditList = Runner.getAuditList;
module.exports.traceCategories = require('./gather/driver').traceCategories;
module.exports.Audit = require('./audits/audit');
module.exports.Gatherer = require('./gather/gatherers/gatherer');

在这个阶段,我的lighthouse.d.ts项目中有一个文件:

declare module "lighthouse" {

  export interface flags {
    port: number;
    disableCpuThrottling: boolean;
    disableDeviceEmulation: boolean;
    disableNetworkThrottling: boolean;
  }

  export default function lighthouse(url: string, flags: flags, perfConfig: any): any
}

它似乎输入正确。如果我使用 导入import * as lighthouse from "lighthouse";,我可以在我需要的地方lighthouse.default做。lighthouse.flags

但是如果我运行我的代码,我会得到TypeError: lighthouse.default is not a function. 我是否被迫使用export =?如果我没有选择,如何使用export =但导出函数、flags类型和getAuditList, traceCategories, Audit, Gatherer?

4

1 回答 1

4

这是 commonjs 模块默认导出某些内容的方式...

module.exports.default = ...;

...并且在代码示例中没有任何迹象。该模块仅导出一个函数并为其分配一些属性。尝试将此作为 .d.ts 的起点:

declare module "lighthouse" { // the module name must be in quotes

    const lighthouse: { // it must be a "const".

        //here starts the signature "(...): any " that describes the object as callable
        (url: string, 
         flags: { 
                     port?: number;                       // all of these flags keys must be "?"
                     disableCpuThrottling?: boolean;      // because the parameter initializer
                     disableDeviceEmulation?: boolean;    // is an empty {}
                     disableNetworkThrottling?: boolean;  //
         } = {}, 
         perfConfig: any): any; 


        //  and here are all the properties that the library has attached to the function.
        getAuditList: ...;    // 
        traceCategories: ...; //  give them the correct type for a better compiler support
        Audit: ...;           // 
        Gatherer: ...;        //
    }

    export = lighthouse; // <-finally
}

这就是它必须在 .ts 文件中使用的方式

import * as lighthouse from "lighthouse";

lighthouse(...);                //<- invoke it directly
...lighthouse.traceCategories...; //<- get access to one of it's properties

使用“export =” 不会将函数包装在对象中,因此转译的 .js 文件将如下所示:

var lighthouse = require("lighthouse");
lighthouse(...); // this is the desired result and not _whateverName.lighthouse(...);
于 2017-11-17T00:17:03.450 回答