我刚刚安装了nodejs。
我想计算平均值,所以我需要 mathjs。我知道我可以使用其他方法,但这很方便。
const mathjs = require('mathjs')
与其他模块(内置模块或我的用户定义模块)不同,我没有看到智能感知弹出,这让我很困扰,因为我看不到所有可用的方法。
我尝试过的事情:
npm install mathjs
(本地安装)npm -g install mathjs
(全局安装)我曾尝试修改 vscode 设置,但由于它适用于其他所有内容,因此我不愿做出太多随机猜测。
我访问了官方页面(https://www.npmjs.com/package/mathjs),但没有看到任何能给我更好线索的东西。所以现在我被卡住了。
我应该补充一点,模块工作正常,只是这个特定模块的智能感知部分不起作用。
这是我最小的可复制示例:
/*
why does the intellisense not work for mathjs ?
*/
const mathjs = require('mathjs') // intellisense does not work
const fs = require('fs') // intellisense does work
let m = mathjs.mean([1,2,3,4,5]) // the actual module method works fine !
let n = Math.max(1,2,3,4,5) // the intelisense works fine here too.
console.log(m)
console.log(n)
我怎样才能让智能感知为这个模块工作(假设它是可能的)?
这是由于您导入
mathjs
的方式,这是由于 require
返回 any
类型,所以它不知道模块具有哪些属性。
要解决这个问题,您只需要以不同的(和首选的)方式导入,为此您只需要导入您将要使用的函数而不是 hole mathjs 模块,这里是您修改的代码示例以说明差异:
import { mean } from "mathjs"
let total = mean([1,2,3,4,5])
现在你的 vscode 将正常工作,你只需要在需要时导入任何你需要的东西。