typescript - 如何为DefiniteTyped 编写React Native 库的测试?
问题描述
我想为 React Native 的rn-placeholder库做出贡献,为DefinitiveTyped提供类型。这是我第一次index.d.ts
向 DefinitiveTyped 贡献文件。
我写了声明文件。现在我想写强制rn-placeholder-tests.ts
文件。但是当我尝试使用我的声明时,我得到了regexp
错误:
[ts]
Conversion of type 'RegExp' to type 'View' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Property 'measure' is missing in type 'RegExp'.
我什至尝试只复制测试@types/react-native
,但它们给出了相同的错误:
import * as React from 'react';
import { Text, View } from "react-native";
class CustomView extends React.Component {
render() {
return <View />
}
}
这是我的tsconfig.json
:
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"jsx": "react",
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.d.ts", "rn-placeholder-tests.ts"]
}
其他文件只是使用npx dts-gen --dt --name my-package-name --template module
.
我怎样才能让测试通过?
编辑:我转换rn-placeholder-tests.ts
为.tsx
which 摆脱了regexp
错误。但是现在 TSLint 抱怨它找不到模块:
[ts] Cannot find module 'react'.
[ts] Cannot find module 'react-native'.
[ts] Cannot find module 'rn-placeholder'.
到底是怎么回事?
解决方案
就文件扩展名而言,将 .ts 更改为 .tsx 是正确的举措。(在此处查看有关 tsx 的更多信息:https ://www.typescriptlang.org/docs/handbook/jsx.html )
对于其他“找不到模块”错误,请参阅此“打字稿:找不到模块 'react'”问题。
可能是您需要添加类型定义:
npm i -D @types/react
或者,也许您只需将“moduleResolution”添加到您的 tsconfig.json 文件中:
"compilerOptions": {
...
"moduleResolution": "node" <---------- this entry
...
}
推荐阅读
- c# - 无法识别 LocalReport 属性的 Visual Studio 中 devexpress 的 ReportViewer 控件
- python - 在满足条件的同时找到与另一个像素相关的最近像素的最快方法。OpenCV Numpy
- sql-server - 如何从sql server中的一组数据中获取第一行数据
- jsf - 如何过滤上传文件的 MIME 类型
- ios - 如果我在新证书上更新我的配置文件(企业分发方法),我的应用程序会停止吗?
- jquery - 每 2 秒滑动一个 div
- jakarta-ee - 如何避免wildfly为JNDI创建依赖项
- javascript - 在堆栈导航器中保存 Switch 组件的状态
- python - 搜索 JSON 文件以获取记录并附加到数据框
- angular - Angular 7 中的单例服务