ris*_*ott 14 node.js npm typescript
在许多情况下,通过专用包或与 npm 包本身一起提供的第三方类型定义@types/*后来被发现不完整或与新的依赖项不兼容。
因此,我正在寻找一种直接的方法来覆盖node_modules. 理想情况下,我只需将它们复制粘贴到我的存储库中,并告诉 TypeScript 编译器使用我的类型即可。
我找到了 3 种可能的解决方案,但没有一个特别好:
declare module 'printer' {
export function print(msg: string): void;
}
declare module 'printer/color' {
export function print(msg: string, color: string): void;
}
// WARNING: If you `import` or `export` anything here,
// this will suddenly *augment*, not *override* the
// 'printer' module.
Run Code Online (Sandbox Code Playgroud)
node_modules因为
declare module 'printer' {}import print from 'printer/color')必须显式声明为单独的模块声明。import/ export,因此需要重写才能在环境模块中工作。declare module 'fs' {}只会增加@types/nodenot override 的类型)declare module 'printer' {
export function print(msg: string): void;
}
declare module 'printer/color' {
export function print(msg: string, color: string): void;
}
// WARNING: If you `import` or `export` anything here,
// this will suddenly *augment*, not *override* the
// 'printer' module.
Run Code Online (Sandbox Code Playgroud)
import bar from 'printer/color')必须显式声明为单独的路径映射。@types/node)// tsconfig.json
{
"baseUrl": "./",
"paths": {
"printer": ["types/printer"],
"printer/color": ["types/printer/color"],
}
}
Run Code Online (Sandbox Code Playgroud)
@types/*包,但它不适合发布自己类型的包,因为您将有效地获得运行时代码的所有权,这意味着您将错过安全修复等。TypeScript 文档似乎没有讨论这个常见问题的解决方案,但我想知道是否有人有一个更强大、警告更少的解决方案?
| 归档时间: |
|
| 查看次数: |
4453 次 |
| 最近记录: |