如何获取 lodash 类型信息以在 VSCode 中工作?
Posted
技术标签:
【中文标题】如何获取 lodash 类型信息以在 VSCode 中工作?【英文标题】:How can I get lodash type info to work in VSCode? 【发布时间】:2019-10-08 09:56:31 【问题描述】:我在 VSCode 中收到一个(弯曲的红线)错误,其中包含以下 .ts
文件:
src\lib\hi.ts
import * as _ from 'lodash';
function testLodash(): void
function square(n: number)
return n * n;
return _.map([4, 8], square);
错误
Property 'map' does not exist on type 'typeof import("lodash")'.ts(2339)
对于任何常用的 lodash 函数,我都会遇到相同的错误。当通过_.
调用时,Intellisense 只会给我Lodash
类。
我猜 VSCode 找不到我的类型定义,但我不明白为什么。
如果我将导入更改为:
import 'lodash';
我得到了预期的功能,错误消失了,但我得到了一个新的错误:
'_' refers to a UMD global, but the current file is a module. Consider adding an import instead.ts(2686)
应用建议的 QuickFix 会将以下内容添加到导入中:
import _ from 'lodash';
但后来我得到了原来的错误。
那么我需要更改哪些内容才能使其正常工作?
以下是详细信息:
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 设置
没有非默认的 TS 设置
TS 版本
版本 3.4.5
VSCode 版本
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
已安装的扩展程序
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
【问题讨论】:
【参考方案1】:我有同样的问题,我通过这种方式解决了它:
const _ = require('lodash');
【讨论】:
我在尝试您的解决方案时遇到了另一个类型错误:const _ : _.LoDashStatic = require('lodash');
修复了该问题。这似乎有点混乱,但它确实有效。
为什么需要_.LoDashStatic
类型声明?
没有类型我得到以下错误:Subsequent variable declarations must have the same type. Variable '_' must be of type 'LoDashStatic', but here has type 'any'.
你可以试试这个github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/…
我已经重新检查了,因为我的 tsconfig 已将 target
更改为 es2015
我可以像这样导入:import * as _ from 'lodash';
以上是关于如何获取 lodash 类型信息以在 VSCode 中工作?的主要内容,如果未能解决你的问题,请参考以下文章