简体   繁体   English

在打字稿中,如何从“ lodash / merge”导入合并

[英]In typescript, how to do import merge from “lodash/merge”

With es6, I can import just one function from a library to minimise the whole bundle, like: 使用es6,我可以从库中仅导入一个函数以最小化整个包,例如:

import merge from "lodash/merge"

However, in typescript, the above sentence will cause a compile error: Cannot find module "lodash/merge". 但是,在打字稿中,以上语句将导致编译错误:找不到模块“ lodash / merge”。 How to solve it? 怎么解决呢?

Cannot find module "lodash/merge". 找不到模块“ lodash /合并”。 How to solve it 如何解决

The definitions for TypeScript don't support individual loading of lodash members. TypeScript的定义不支持lodash成员的单独加载。

Fix 固定

Either import * as _ from "lodash" or write the definitions yourself. import * as _ from "lodash"或自己编写定义。

MORE 更多

TIPs on authoring : https://basarat.gitbooks.io/typescript/content/docs/types/ambient/intro.html 创作技巧: https : //basarat.gitbooks.io/typescript/content/docs/types/ambient/intro.html

Actually you can import a single function using the 'lodash.merge' library 实际上,您可以使用'lodash.merge'库导入单个函数

import merge from 'lodash.merge'

const result: any = merge(obj1, obj2)

BUT

You need to add this property to the tsconfig.json "esModuleInterop": true without this it will not work 您需要将此属性添加到tsconfig.json "esModuleInterop": true没有, "esModuleInterop": true它将不起作用

[cit] https://github.com/lodash/lodash/issues/3192#issuecomment-411963038 [cit] https://github.com/lodash/lodash/issues/3192#issuecomment-411963038

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

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