我是 Typescript 和 NodeJs 的新手。每当我在 package.json
中提到一个 Node 模块时在导入 Node 模块时,我总是收到以下错误。
Could not find a declaration file for module 'ini'. 'e:/typescript-2020-1/parser1/node_modules/ini/ini.js' implicitly has an 'any' type.
Try `npm install @types/ini` if it exists or add a new declaration (.d.ts) file containing `declare module 'ini';`
Could not find a declaration file for module 'json-query'. 'e:/typescript-2020-1/parser1/node_modules/json-query/index.js' implicitly has an 'any' type.
Try `npm install @types/json-query` if it exists or add a new declaration (.d.ts) file containing `declare module 'json-query';`
为了解决这个问题,我总是在
package.json
中搜索并安装以下内容如下所示。"dependencies": {
"ini": "^1.3.5",
"@types/ini": "^1.3.30",
"json-query": "^2.2.2",
"@types/json-query": "^2.2.0"
"@types/node": "^13.9.0"
}
如何避免包含所有类型的 Node 模块特定类型。
是否有可能以
@types/<node module>
的方式进行?将自动添加到 node_modues 中,而不直接添加到 package.json
?我尝试在
tsconfig.json
中添加以下内容也,但它不工作。"typeRoots": [
"node_modules/@types"
],
我的问题再次是“是否总是需要为每个 Node 模块添加
@types
?请帮我解决它。
最佳答案
如果 npm 包包含类型,您将在安装时免费获得它们。
如果没有,那么您可以在 npm 的 @types 命名空间下找到它们。
有一个有用的工具叫做 typac您可以通过 npx
安装或运行当您使用它来安装任何 npm 包时,它将尝试为您的包安装 @types。
IE。
npx typac ini -i
关于node.js - 如何为所有 Node 模块自动安装@types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60575796/