类型“Element”不可分配给类型“string”.ts(2322)

6 javascript function typescript reactjs react-hooks

我是新的反应打字稿开发人员,这里我有一个可以在“js”中工作的代码,但是当更改为“tsx”时出现错误:类型“Element”不可分配给类型“string”.ts(2322)它指向对于“helperTextt”错误(在其他地方找不到答案)我应该做什么:let helperTextt:any = ""; ?

有什么建议 ?

let helperTextt = "";

if (event.target.id === "hostname") {
  if (!HOSTNAME_REGEX.test(event.target.value)) {
    helperTextt = (
      <Trans i18nKey="form.formData.hostNameError">
        Invalid Host name
      </Trans>
    );
    errorr = true;
  }
}
Run Code Online (Sandbox Code Playgroud)

Gab*_*oli 11

你可以做

import { ReactElement } from "react";
Run Code Online (Sandbox Code Playgroud)

进而

let helperTextt: ReactElement | null = null;
Run Code Online (Sandbox Code Playgroud)


小智 1

您可以向helperTextt 变量添加类型

let helperTextt: JSX.Element = null;
Run Code Online (Sandbox Code Playgroud)