简体   繁体   中英

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:

import merge from "lodash/merge"

However, in typescript, the above sentence will cause a compile error: Cannot find module "lodash/merge". How to solve it?

Cannot find module "lodash/merge". How to solve it

The definitions for TypeScript don't support individual loading of lodash members.

Fix

Either import * as _ from "lodash" or write the definitions yourself.

MORE

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

Actually you can import a single function using the 'lodash.merge' library

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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