node.js - 如何为所有 Node 模块自动安装@types

标签 node.js angular typescript npm

我是 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/

相关文章:

javascript - 在 hAxis google 图表栏中显示所有标签

javascript - TypeScript 为 HTMLCollectionOf<Element>、NodeCollection<Element>、google 自动完成表单设置 css 样式

javascript - Later.js 按计划执行功能?

node.js - 使用对象更新准备好的语句

angular - Angular 2 Bootstrap 日期选择器的输入未设置值

javascript - 带有 Spring Boot 的 Thymeleaf 或 Angular 4

angularjs - 路由更改时重新加载 Angular 2 应用程序

typescript - 带有类型谓词的参数的窄类型

javascript - 错误 : data and salt arguments required

javascript - 使用 Makefile 构建 javascript