question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing lodash in a Typescript project rises an editor error

See original GitHub issue

I’m trying to use lodash inside a Typescript, but so far I have been unable to do so. The main issue seems to be related to Typescript typing system.

dependencies: lodash@4.17.5 @types/lodash@4.14.104

Error: Property 'any lodash property' does not exist on type 'LoDashStatic'.

  • I try to uninstall lodash, and install typing module first, but I did not solve the issue.
  • I try import * as _ from 'lodash' and import _ from 'lodash'. Neither solves the error.
  • Using import * as _ from 'lodash' does not even run the code.
  • Using import _ from 'lodash' does run the code, but the error still in the editor.

I also notice that there is no errors in the console when running the application. I don’t know if that’s by design, but it should say something.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
kylecordescommented, Mar 10, 2018

@michaeljota Yes now I see. The editor shows a red mark because it does not recognize the lodash import for TypeScript. Adding the types does not work; it just shifts the error to the left portion of the import. To get good results, with the style of importing individual operators, I had to:

  • Switch to using lodash-es (add the NPM dependency, and change the import)
  • also import @types/lodash-es

you can see it in use here:

https://stackblitz.com/edit/github-av3nlg?file=src/app/reddit-search/reddit-image-search.service.ts

While unfortunate, I don’t know of a way to avoid it for this. I think there is a chasm in thinking between two different groups of NPM package creators

  • One group is accustomed to newer packages, like the way Angular is packaged. With this style of packaging, the goal is to have just a single package which provides whatever common bundle and language level variance are needed. Angular calls this the Angular Package Format, but prior to Angular some other package creators have had similar goals.
  • Another group sees NPM as primarily, by default, about CommonJS/ES5 code; and treats anything other than that is a special case, requiring a special, different package name. In this case, lodash-es instead of lodash.

I hope the newer style gains popularity.

0reactions
sevevescommented, Mar 22, 2018

Please see my comment in #338 … you can use lodash-es instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fixing Lodash TypeScript Errors By Upgrading @types/lodash ...
My main file would then import this lodash-extended module and try to use it: // Import the core node services. import ...
Read more >
typescript fails to import lodash - Stack Overflow
I have a project that uses typescript and runs in node. I have lodash in my dependencies of the package.json installed.
Read more >
A Complete Guide to Using TypeScript in Node.js - Better Stack
A Complete Guide to Using TypeScript in Node.js · Step 1 — Downloading the demo project · Step 2 — Installing and configuring...
Read more >
PhpStorm - Code Inspections in JavaScript and TypeScript
You can toggle specific inspections or change their severity level on the Editor | Inspections page of the IDE settings Ctrl+Alt+S . DOM...
Read more >
rollup.js
Importing CommonJS · Publishing ES Modules ... reduces the complexity of the problems you'll need to solve, and simply writing smaller projects in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found