如何自动为所有节点模块安装@types

Deb*_*eba 12 node.js npm typescript angular

我是 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';`
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我总是在下面搜索并安装以下内容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"
    }
Run Code Online (Sandbox Code Playgroud)

如何避免包含所有类型的节点模块特定类型。是否可以在@types/<node module>不直接添加到的情况下自动添加到 node_modues 中package.json?我也尝试添加以下内容tsconfig.json,但它不起作用。

"typeRoots": [
            "node_modules/@types"
        ],
Run Code Online (Sandbox Code Playgroud)

我的问题再次是“是否总是需要@types为每个节点模块添加?请帮我解决它。

mar*_*ght 5

如果 npm 包包含类型,您将在安装时免费获得它们。

如果没有,那么您可以在 npm 的 @types 命名空间下找到它们。

有一个名为typac的有用工具,您可以通过npx它安装或运行它,当您使用它安装任何 npm 包时,它会尝试为您的包安装 @types。

IE

    npx typac ini -i  
Run Code Online (Sandbox Code Playgroud)

  • 此“ini”命令和“-i”标志在最新版本中无效。我找不到替代品,因此该解决方案不再有效。 (6认同)