【问题标题】:re-export lodash exports in typescript在打字稿中重新导出 lodash 导出
【发布时间】:2016-10-27 09:19:37
【问题描述】:

我在我的 typescript 项目中使用 lodash,并为 lodash 键入内容。 此外,我还有一些私有 npm 模块,其中包含我在不同项目中使用的实用程序。 它导出这样的方法:

export * from './src/stringStuff';
export * from './src/httpStuff';

我想做的是为 lodash 提供该 utils 存储库。所以它看起来像这样:

import {
    forEach
} from '@foo/utils'

forEach 是一个 lodash 方法。 我的方法是在我的 index utils 文件中添加一个类似这样的 lodash 导出:

export * from 'utils';
export * from './src/stringStuff';
export * from './src/httpStuff';

现在我可以像上面的例子一样导入和使用例如forEach。 但问题是类型脚本编译器给了我一个错误 /files/@foo/utils/index.ts has no exportet member forEach:

ERROR in [default] /files/@foo/utils/index.ts:1:14 
Module '"lodash"' uses 'export =' and cannot be used with 'export *'.

ERROR in [default] /files/@foo/utils/index.ts:24:4 
Module '"/files/@foo/utils/index.ts"' has no exported member 'forEach'.

对我来说很奇怪,我可以使用forEach,但也会收到该错误消息。为什么会这样,我该如何解决?是导入/导出错误还是输入文件错误?

【问题讨论】:

    标签: javascript node.js typescript


    【解决方案1】:

    我发现关于不同库结构的文本: https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html

    在我看来,最好将 lodash 定义为该示例文件中的模块,因为我猜它更像是一个 UMD 模块?! https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html

    或者在这里: https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#support-for-umd-module-definitions

    解决了我用我自己的类型文件重新定义 lodash 的问题,但我不知道是否还有其他解决方案...

    【讨论】:

      猜你喜欢
      • 2016-01-23
      • 2020-06-29
      • 2017-02-06
      • 1970-01-01
      • 2017-08-07
      • 2023-02-02
      • 1970-01-01
      • 2021-12-19
      • 2022-01-10
      相关资源
      最近更新 更多