简体   繁体   English

用打字稿重新导出lodash导出

[英]re-export lodash exports in typescript

I'm using lodash in my typescript project with typings for lodash. 我在打字稿项目中使用lodash,并为lodash键入内容。 Besides I have some private npm module with utils I'm using in different projects. 此外,我还有一些在不同项目中使用的utils私有npm模块。 It exports methods like that: 它导出如下方法:

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

Want I want to do is providing lodash with that utils repo. 我想要做的就是为lodash提供该utils回购。 So it would look like that: 所以看起来像这样:

import {
    forEach
} from '@foo/utils'

where forEach is a lodash method. 其中forEach是lodash方法。 My approach was to add a lodash export like that in my index utils file: 我的方法是在索引utils文件中添加lodash导出:

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

Now I can import and use for example forEach like in the example above. 现在,像上面的示例一样,我可以导入和使用例如forEach BUT the thing is that the type script compiler gives me an error that /files/@foo/utils/index.ts has no exportet member forEach : 但是问题是类型脚本编译器给我一个错误,即/files/@foo/utils/index.ts没有forEach /files/@foo/utils/index.ts成员:

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'.

For me it's weird that I can use forEach but also get that error message. 对我来说,我可以使用forEach但也收到该错误消息,这很奇怪。 Why is that and how can I fix it? 为什么会这样,我该如何解决? Is it a wrong import/export or maybe the typing file is wrong? 是错误的导入/导出还是输入文件错误?

I found that text about different library structures: https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html 我发现有关不同库结构的文本: https : //www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html

And it seems to me like it's better to define lodash as a module like in that example file because I guess it's more like a UMD module?! 在我看来,最好将lodash定义为该示例文件中的模块,因为我想它更像是UMD模块? https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html

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

Solved my problem to redefine lodash with my own typings file but I don't know if there's another solution... 解决了我用自己的类型文件重新定义lodash的问题,但我不知道是否还有其他解决方案...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM