首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >正确地在类型记录中使用export =和export默认值来创建灯塔的类型

正确地在类型记录中使用export =和export默认值来创建灯塔的类型
EN

Stack Overflow用户
提问于 2017-11-16 10:34:04
回答 1查看 3.5K关注 0票数 1

我正在尝试为这个灯塔文件创建一个.d.ts,并且我很难理解如何处理默认的导出

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

代码语言:javascript
运行
复制
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文件:

代码语言:javascript
运行
复制
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.defaultlighthouse.flags

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-17 00:17:03

这就是公共as模块导出默认内容的方式.

代码语言:javascript
运行
复制
module.exports.default = ...;

..。在代码样本中没有它的迹象。该模块只导出一个函数并为其分配一些属性。尝试将此作为.d.ts的起点:

代码语言:javascript
运行
复制
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文件中必须使用的方法。

代码语言:javascript
运行
复制
import * as lighthouse from "lighthouse";

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

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

代码语言:javascript
运行
复制
var lighthouse = require("lighthouse");
lighthouse(...); // this is the desired result and not _whateverName.lighthouse(...);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47327369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档