node.js - 如何自動為所有 Node 模組安裝 @types
我對 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_modules 中,而不直接添加到 package.json?
我也嘗試在 tsconfig.json 中添加以下內容,但它沒有作用。
"typeRoots": [
"node_modules/@types"
],
我再次提出我的問題:「是否每個 Node 模組都必須添加 @types?」
答案#1
如果一個 npm 套件包含型別,當你安裝時就會免費獲得它們。
如果沒有,那你可能可以在 npm 的 @types 命名空間下找到它們。
有一個叫做 typac 的實用工具,你可以透過 npx 安裝或運行它,當你使用它來安裝任何 npm 套件時,它會嘗試安裝該套件的 @types。
即
npx typac ini -i
答案#2
typesync為你的 package.json 中的依賴項安裝缺失的 TypeScript 類型定義。
https://github.com/jeffijoe/typesync
(typac 開發似乎已經停滯。)
