首页 > 解决方案 > 除非'--jsx'-React 否则不能使用 JSX

问题描述

    {
      "compilerOptions": {
        "target": "es5",
        "lib": [
          "dom",
          "dom.iterable",
          "esnext"
        ],
        "allowJs": true,
        "skipLibCheck": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "noFallthroughCasesInSwitch": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "noEmit": true,
        "jsx": "react" <=error
      },
      "include": [
        "./src/**/*.ts"
      ]
    }

登录.tsx

   interface IUserLoginInformation {
      username: string;
      password: string;
    }

    const Login = () => {
      const {
        register,
        handleSubmit,
        watch,
        formState: { errors }
      } = useForm<IUserLoginInformation>();
      const history = useHistory();
    
      const [users, setUsers] = React.useState([]);
      
    
      const onSubmit: SubmitHandler<IUserLoginInformation> = data =>
        fetchLoginDeatils();
    
      return (
        <form onSubmit={handleSubmit(onSubmit)}>
          <input defaultValue="test" {...register("username")} />{" "}
          {/* register your input into the hook by invoking the "register" function */}
          {/* include validation with required or other standard HTML validation rules */}
          <input {...register("password", { required: true })} />
          {/* errors will return when field validation fails  */}
          {errors.password && <span>This field is required</span>}
          <input type="submit" />
        </form>
      );
    };
    export default (Login);

在上面的代码中,我收到以下错误。我尝试了互联网上所有可能的选项。但没有任何改变。在登录代码中,我同时使用 javascript 和类型脚本。我也将“jsx”:“react-jsx”更改为“jsx”:“ react” 。我在 tsconfig 出现错误的地方做了标记。 js

 Cannot use JSX unless the '--jsx' flag is provided.

标签: reactjstypescript

解决方案


以下是错误的,因为它排除了.tsxLogin.tsx

  "include": [
    "./src/**/*.ts"
  ]

使固定

将其更改为:

  "include": [
    "src"
  ]

其他提示

  • 更改后尝试重新启动 IDE + 终端以获取 tsconfig.json。
  • 确保没有其他 tsconfig.json 文件。

推荐阅读