John Walker
John Walker

Reputation: 543

How can I get lodash type info to work in VSCode?

I am getting a (squiggly red line) error in VSCode with the following .ts file:

src\lib\hi.ts

import * as _ from 'lodash';

function testLodash(): void {

  function square(n: number) {
    return n * n;
  }

  return _.map([4, 8], square);
}

Error

Property 'map' does not exist on type 'typeof import("lodash")'.ts(2339)

I get the same error for any of the usual lodash functions. Intellisense only gives me the Lodash class when invoked via _.. I am guessing that VSCode can't find my type definitions but I can't see why.

If I change the import to:

import 'lodash';

I get the expected functions and the error goes away but i get a new error:

'_' refers to a UMD global, but the current file is a module. Consider adding an import instead.ts(2686)

Applying the suggested QuickFix adds the following to imports:

import _ from 'lodash';

But then I get the original error back.

So what do I need to change to make this work?

Here are the details:

package.json

{
  ...
  "dependencies": {
    ...
    "lodash": "^4.17.11",
    ...
  },
  "devDependencies": {
    ...
    "@types/lodash": "^4.14.130",
    ...
    "typescript": "^3.4.5"
  }
}

tsconfig.json

{
  "compilerOptions": {
     "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "allowJs": true,                       /* Allow javascript files to be compiled. */
    "outDir": "./dist/",                        /* Redirect output structure to the directory. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "esModuleInterop": true                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
  }
}

VSCode Settings

No non-default TS settings

TS Version

Version 3.4.5

VSCode Version

Version: 1.34.0 (user setup)
Commit: a622c65b2c713c890fcf4fbf07cf34049d5fe758
Date: 2019-05-15T21:59:37.030Z
Electron: 3.1.8
Chrome: 66.0.3359.181
Node.js: 10.2.0
V8: 6.6.346.32
OS: Windows_NT x64 10.0.17134

Installed Extensions

alanz.vscode-hie-server
alefragnani.Bookmarks
andyyaldoo.vscode-json
christian-kohler.path-intellisense
daiyy.quick-html-previewer
eamodio.gitlens
Edka.haskutil
esbenp.prettier-vscode
fabiospampinato.vscode-todo-plus
flowtype.flow-for-vscode
gcazaciuc.vscode-flow-ide
hoovercj.haskell-linter
jcanero.hoogle-vscode
justusadam.language-haskell
karigari.chat
mrmlnc.vscode-duplicate
ms-vscode.vscode-typescript-tslint-plugin
ms-vsliveshare.vsliveshare
ms-vsliveshare.vsliveshare-audio
ms-vsliveshare.vsliveshare-pack
phoityne.phoityne-vscode
SirTobi.code-clip-ring
WakaTime.vscode-wakatime

Upvotes: 4

Views: 4899

Answers (1)

Scofjeld
Scofjeld

Reputation: 272

I have same issue, and I get through it by this way:

const _ = require('lodash');

Upvotes: 3

Related Questions