Home > Software design >  Type 'Element' is not assignable to type 'string'.ts(2322)
Type 'Element' is not assignable to type 'string'.ts(2322)

Time:09-17

I'm new react typescript developer, here i have a code which works in 'js' 'but when changed to 'tsx' getting error: Type 'Element' is not assignable to type 'string'.ts(2322) it is pointing to 'helperTextt' error (did not find answer else where) should i do : let helperTextt:any = ""; ?

any suggestions ?

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;
  }
}

CodePudding user response:

you can add type to your helperTextt variable

let helperTextt: JSX.Element = null;

CodePudding user response:

You could do

import { ReactElement } from "react";

and then

let helperTextt: ReactElement | null = null;

CodePudding user response:

Typescript is automatically inferring helperTexttto be a string, if you are certain of its values you can get around this (although it is not preferred to declare type any) by declaring it as type any:

 let helperTextt:any;
  • Related