测试库中函数的通用类型

generic type for fucntion from test-library

我有如下部分代码,但在类型检查错误 Don't use 'Function' as a type. The 'Function' type accepts any function-like value. 时失败。我从 testing-library/react 提供函数类型的最佳方式是什么?

async function onAction(
  queryByTestId: Function,
  findByTestId: Function,
): Promise<void> {
  await waitFor(() => expect(queryByTestId(selector)).toBeVisible());
  const triggerChange = await findByTestId('NAME');
}

const {findByTestId, queryByTestId} = render(<Component />);
await onAction(queryByTestId, findByTestId);

Function类型是一个非常宽泛的类型,类似于Object类型的大小。建议定义函数定义的样子。

根据您提供的代码量,可能会出现类似这样的内容:

async function onAction(
  queryByTestId: (selector: string) => Element | null,
  findByTestId: (selector: string) => Promise<Element | null>,
): Promise<void> {
// etc

或者,如果您有权访问函数或类型文件,您可以在那里定义结构,导出定义并在此文件中使用它。

可以在 Typescript Documentation - More on Functions

找到更多关于函数类型的阅读材料