javascript - React și TypeScript, apare eroarea „Se așteaptă tip”
Îmi convertez un component React în Typescript (*.ts) din JS. Îmi dă eroarea de mai jos „Type expected”. Cum aș putea să o rezolv?
const SisenseDashboard = () => {
const sisenseRef = React.useRef(null);
return (
<>
<Box
display="flex"
flexDirection="column"
alignItems="center"
marginBottom={2}
>
<h2>In-App Reporting</h2>
</Box>
<Box
ref={sisenseRef}
id="sisense-container"
className="sisense-demo"
></Box>
</>
);
};
Răspuns #1
Asigură-te că extensia fișierului componentului tău este .tsx și nu .ts. Aceasta ar trebui să rezolve problema ta. În plus, ai putea spune TypeScript că funcția ta este un Component React Funcțional, de exemplu, cu ajutorul tipului FC din React:
import {FC} from "react";
const SisenseDashboard : FC = () => {
const sisenseRef = React.useRef(null);
return (
<>
<Box
display="flex"
flexDirection="column"
alignItems="center"
marginBottom={2}
>
<h2>In-App Reporting</h2>
</Box>
<Box
ref={sisenseRef}
id="sisense-container"
className="sisense-demo"
></Box>
</>
);
};
