如何为 DefinitelyTyped 的 React Native 库编写测试?
How to write the tests for a React Native library for DefinitelyTyped?
我想为 rn-placeholder library for React Native by providing the types for DefinitelyTyped 做贡献。这是我第一次向 DefinitelyTyped 贡献 index.d.ts
文件。
我写了声明文件。现在我想写强制性的 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
,从而消除了 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)
对于其他 "Cannot find module" 个错误,请参阅此 。
您可能需要添加类型定义:
npm i -D @types/react
或者,也许您只需要将 "moduleResolution" 添加到 tsconfig.json 文件中:
"compilerOptions": {
...
"moduleResolution": "node" <---------- this entry
...
}
我想为 rn-placeholder library for React Native by providing the types for DefinitelyTyped 做贡献。这是我第一次向 DefinitelyTyped 贡献 index.d.ts
文件。
我写了声明文件。现在我想写强制性的 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
,从而消除了 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)
对于其他 "Cannot find module" 个错误,请参阅此
您可能需要添加类型定义:
npm i -D @types/react
或者,也许您只需要将 "moduleResolution" 添加到 tsconfig.json 文件中:
"compilerOptions": {
...
"moduleResolution": "node" <---------- this entry
...
}