【问题标题】:Augment imported module declaration增加导入的模块声明
【发布时间】:2020-03-16 18:35:37
【问题描述】:

我在我的项目中同时安装了webpack-bundle-analyzer@types/webpack-bundle-analyzer,我可以说提供的声明工作正常,但是这些声明并不完整。除了BundleAnalyzerPlugin类,库还导出了start()函数。

如何扩充现有声明以添加start() 函数?

我尝试创建以下文件:./types/webpack-bundle-analyzer/index.d.ts,其内容如下:

import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';

declare module 'webpack-bundle-analyzer' {

  export function start(
    bundleStats: any,
    opts: BundleAnalyzerPlugin.Options
  );

}

但由于某种原因它并没有改变任何东西。

我的tsconfig.json 设置了typeRoots 选项:

{
  "compilerOptions": {
    "typeRoots": [
      "node_modules/@types/",
      "types/"
    ]
  }
}

我收到以下错误:

src/somefile.ts:4:10 - error TS2305: Module '"../../node_modules/@types/webpack-bundle-analyzer"' has no exported member 'start'.

4 import { start } from 'webpack-bundle-analyzer';

原始声明是这样定义的:

import { Plugin, Compiler } from 'webpack';

export namespace BundleAnalyzerPlugin {
    interface Options { … }
}

export class BundleAnalyzerPlugin extends Plugin {
    constructor(options?: BundleAnalyzerPlugin.Options);
    apply(compiler: Compiler): void;
}

【问题讨论】:

  • 模块扩充应作为常规文件/模块包含

标签: typescript typescript-typings


【解决方案1】:

问题在于我的tsconfig.json 的写法:

{
  "compilerOptions": {
    "typeRoots": [
      "node_modules/@types/",
      "types/"
    ]
  }
}

我需要更改搜索目录的顺序,以便我的本地声明优先于从 node_modules 导入的声明:

{
  "compilerOptions": {
    "typeRoots": [
      "types/",
      "node_modules/@types/"
    ]
  }
}

【讨论】:

  • 我花了一整天的时间试图理解为什么类型增强在我的项目中不起作用。确实需要更好地记录此功能。不管怎样,谢谢你的回答,绝对的救命稻草。
猜你喜欢
  • 2021-11-04
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 2018-06-25
  • 2017-09-25
  • 2017-12-28
  • 2016-12-26
  • 1970-01-01
相关资源
最近更新 更多