【问题标题】:App' refers to a value, but is being used as a type here. Did you mean 'typeof App'?App' 指的是一个值,但在这里被用作一个类型。您的意思是“typeof App”吗?
【发布时间】:2021-08-02 10:23:11
【问题描述】:

我想边做边学 Typescript 和 React,所以我在 create-react-app 中手动从 JS 迁移到 TS,但我的默认测试文件 App.test.ts


import { render, screen } from '@testing-library/react';
import App from './App';

test('renders learn react link', () => {
  render(<App />); // There's an error here
  const linkElement = screen.getByText(/learn react/i);
  expect(linkElement).toBeInTheDocument();
});

有这个错误:

'App' refers to a value, but is being used as a type here. Did you mean 'typeof App'?

【问题讨论】:

  • 您需要使用.tsx文件扩展名来启用JSX解析。
  • 但这不是一个 React 组件,它是一个测试文件。
  • 当然,但是&lt;App /&gt; 不是有效的 javascript,它是 JSX。您需要使用 .tsx 文件扩展名告诉 typescript 将其解析为 JSX 并将其转换为对 React.createComponent 的调用。

标签: reactjs typescript


【解决方案1】:

只需将文件扩展名从 .ts 更改为 ..tsx 这使得 ` 可以被解析为 JSX 而不是常规的 JavaScript。为我工作。

【讨论】:

    猜你喜欢
    • 2021-07-07
    • 1970-01-01
    • 2020-12-22
    • 2021-01-12
    • 2020-11-01
    • 2021-07-12
    • 2020-03-23
    • 2019-08-29
    • 1970-01-01
    相关资源
    最近更新 更多