我应该在哪里放置自定义的.d.ts文件? [英] Where should I place custom .d.ts files?
本文介绍了我应该在哪里放置自定义的.d.ts文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为没有类型的包提供类型:
error TS7016: Could not find a declaration file for module 'inputmask-core'. './node_modules/inputmask-core/lib/index.js' implicitly has an 'any' type.
Try `npm install @types/inputmask-core` if it exists or add a new declaration (.d.ts) file containing `declare module 'inputmask-core';`
我在类型脚本为2.4.2的webpack中使用ts-Loader,并且我在tsconfig.json中设置了以下类型根:
"typeRoots": [
"./node_modules/@types",
"./src/client/types"
]
我尝试模仿node_modules/@types
中的包结构:
src/client/types
|--inputmask-core
|--index.d.ts
index.d.ts
中包含以下内容:
declare class InputMask {}
export default InputMask;
但错误仍然存在。我做错了什么?我应该将这些自定义.d.ts文件放在哪里?
node_modules/@types
与任何其他类型的根有什么区别?为什么TypeScrip对待他们的方式不同?
推荐答案
使用路径而不是类型根
- https://github.com/Microsoft/TypeScript/issues/22217#issuecomment-369783776
- https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping
";typeRoots";用于全局代码。即某件事 在全局命名空间中声明,并且您希望将其包括在内。为 模块,它们有自己的作用域,您所需要的只是路径映射。 类似于:
{
"compilerOptions": {
"target": "es2017",
"baseUrl": "./",
"paths": {
"*": [ "src/client/@custom_types/*"]
}
},
"exclude": [ "node_modules", "src/client/@custom_types", ... ]
}
注意:路径需要BasUrl,并且您可能还希望添加要排除的目录。
这篇关于我应该在哪里放置自定义的.d.ts文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文