处理未定义声明文件的正确方法(index.d.ts) [英] Correct way to handle undefined declaration files (index.d.ts)

查看:322
本文介绍了处理未定义声明文件的正确方法(index.d.ts)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出现以下错误

 错误 TS7016:找不到模块react-native-camera"的声明文件.'/Users/ilja/Documents/Repositories/blok/node_modules/react-native-camera/index.js' 隐式具有 'any' 类型.尝试 `npm install @types/react-native-camera` 如果它存在或添加一个包含 `declare module 'react-native-camera' 的新声明 (.d.ts) 文件;`

作为快速修复,我在项目的根目录创建了 typings/index.d.ts(没有 @types/react-native-camera)文件并用

填充它

声明模块'react-native-camera';

然后,在我的 tsconfig 文件中,我将它添加到我的类型根目录中

"typeRoots": ["node_modules/@types", "./typings"],

但是我在构建时仍然遇到同样的错误,导致我认为我的实现不正确,我错过了什么?

编辑,我完整的 tsconfig 看起来像这样

<代码>{编译器选项":{"目标": "ES6","module": "commonjs","lib": ["es7"],"allowJs": 真,checkJs":真,"jsx": "反应原生","removeComments": 真,"outDir": "./dist","typeRoots": ["node_modules/@types", "./typings"],"experimentalDecorators": 真,"noFallthroughCasesInSwitch": 真,"noImplicitReturns": 真,"noUnusedLocals": 真,noUnusedParameters":真,allowSyntheticDefaultImports":真,严格":真实},"排除": ["./node_modules", "./android", "./ios", "./assets", "./__tests__", "./dist"],包括":[./src"]}

解决方案

typeRoots 选项指定在何处查找可能包含类型信息的.文档 一直说编译器正在寻找.

我已经复制了您描述的结构并遇到了与您相同的错误.然后我在 typings 下创建了一个 react-native-camera 并将 index.d.ts 移到那里,这样我就得到了这个结构:

typings/└── react-native-camera└── index.d.ts

使用这种结构,编译运行得很好.

使用 typings 目录结构很好,如果你想要那种结构的话.对于某些项目,我使用 typings 目录.对于其他项目,我更喜欢更简单的东西:源树中的 .d.ts 文件,它声明了所有需要声明的内容.因此,如果我从您提供的描述开始,但转储 typings 并添加 src/definitions.d.ts 包含

声明模块'react-native-camera';

然后代码编译就好了.

使用什么确实是一个偏好问题.通常,当项目变得如此复杂以至于 src/definitions.d.ts 最终难以管理或难以理解时,我会转而使用 typings 目录.>

I got following error

error TS7016: Could not find a declaration file for module 'react-native-camera'. '/Users/ilja/Documents/Repositories/blok/node_modules/react-native-camera/index.js' implicitly has an 'any' type.
  Try `npm install @types/react-native-camera` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-native-camera';`

As a quick fix I created typings/index.d.ts (there is no @types/react-native-camera) file at root of my project and populated it with

declare module 'react-native-camera';

then, in my tsconfig file I added it to my type roots like this

"typeRoots": ["node_modules/@types", "./typings"],

however I am still getting same error when I build, leading me to think that my implementation is incorrect, what did I miss?

Edit, my full tsconfig looks like this

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "lib": ["es7"],
    "allowJs": true,
    "checkJs": true,
    "jsx": "react-native",
    "removeComments": true,
    "outDir": "./dist",
    "typeRoots": ["node_modules/@types", "./typings"],
    "experimentalDecorators": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "allowSyntheticDefaultImports": true,
    "strict": true
  },
  "exclude": ["./node_modules", "./android", "./ios", "./assets", "./__tests__", "./dist"],
  "include": ["./src"]
}

解决方案

The typeRoots options specifies where to look for packages that may contain type information. The documentation keeps saying that the compiler is looking for packages.

I've reproduced the structure you described and got the same error you did. Then I created a react-native-camera under typings and moved index.d.ts there so that I end up with this structure:

typings/
└── react-native-camera
    └── index.d.ts

With this structure, the compilation runs just fine.

Using a typings directory structure is fine, if you want that kind of structure. For some projects I use a typings directory. For other projects, I prefer something simpler: a .d.ts file in the source tree that declares everything that needs declaring. So if I start from the description you give, but dump typings and instead add src/definitions.d.ts containing

declare module 'react-native-camera';

then the code compiles just fine.

It is really a matter of preference what to use. Usually when a project becomes so complex that src/definitions.d.ts ends up being difficult to manage or hard to understand, I move to using a typings directory.

这篇关于处理未定义声明文件的正确方法(index.d.ts)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆