问题描述
说我想在我的 TypeScript 项目中使用 dotenv
模块并使用 npm install @types/dotenv --save
安装它的 .d.ts.然后我意识到类型不正确.例如,config()
函数不返回布尔值,而是返回更丰富的对象.
Say that I want to use dotenv
module in my TypeScript project and install its .d.ts using npm install @types/dotenv --save
. Then I realize that the types are not correct. For example, the config()
function doesn't return boolean but a richer object.
我该如何处理这种情况?我是否应该将下载的类型定义复制到另一个文件,手动更新并卸载@types/dotenv?有没有更好的办法?(我需要立即修复,而不是在上游维护者合并之后.)
How do I deal with this situation? Should I just copy the downloaded type definition to another file, update it manually and uninstall @types/dotenv? Is there a better way? (I need the fix right away, not after it has been merged by upstream maintainers.)
推荐答案
我会检查 dotenv
的版本和 @types/dotenv
的版本是否对齐,这可能是功能缺失的原因.
I would check that the version of dotenv
and the version of @types/dotenv
are aligned, that may be the cause of the function missing.
如果是,那么更简洁的方法是自己修改 .d.ts.
If they are then the cleaner way would be to modify the .d.ts yourself.
为了做到这一点:npm remove @types/dotenv
.在您的项目上创建一个文件夹 types
.将 node_modules/@types
中的整个 文件夹 dotenv
复制到其中.
In order to do this: npm remove @types/dotenv
. Create a folder types
on your project. Copy the whole folder dotenv
found in node_modules/@types
into it.
然后在其中修复您的 d.ts 并修改您的 tsconfig.json
以告诉它也在您的新文件夹中查找带有 typeRoots
的缺失类型,如下所示:
Then fix your d.ts in it and modify your tsconfig.json
to tell it to also look in your new folder for missing types with typeRoots
like this:
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"typeRoots": [
"./node_modules/@types",
"./types/",
]
},
"files": ["./app.ts"]
}
(不要忘记添加 ./node_modules/@types
或其他使用 npm 获得的类型,这些类型将不再被找到.)
(Don't forget to add ./node_modules/@types
or other types you got with npm that won't be found anymore.)
希望有帮助!
这篇关于如何覆盖通过@types/package 安装的不正确的 TypeScript 类型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!