import "./Clientes.css"; import "../Cadastrar.css"; import ClientesFisica from…
- User
import "./Clientes.css"; import "../Cadastrar.css"; import ClientesFisica from "./Clientes-Fisica"; import ClientesJuridica from "./Clientes-Juridica"; import { useState } from "react"; import { Link } from "react-router-dom"; import { RadioGroup, FormControlLabel, Radio, FormControl } from "@mui/material";
function Cadastrar() { const [tipoCadastro, setTipoCadastro] = useState("fisica");
const handleTipoCadastroChange = (event) => { setTipoCadastro(event.target.value); };
return ( <div style={{ textAlign: "center" }}> <FormControl component="fieldset" style={{ marginBottom: "20px" }}> <FormControlLabel value="fisica" control={} label="Pessoa Física" /> <FormControlLabel value="juridica" control={} label="Pessoa Jurídica" />
<div className="Voltar"> <Link to="../Cadastrar">Voltar</Link> </div> <div> {tipoCadastro === "fisica" && <ClientesFisica />} {tipoCadastro === "juridica" && <ClientesJuridica />} </div> </div>); }
export default Cadastrar; [{ "resource": "/home/caua/Documentos/Software que eu esqueci oque/Tauri/Importacao-e-Venda/src/Cadastrar/Clientes.tsx", "owner": "typescript", "code": "7006", "severity": 8, "message": "Parameter 'event' implicitly has an 'any' type.", "source": "ts", "startLineNumber": 12, "startColumn": 37, "endLineNumber": 12, "endColumn": 42 }]
- Assistant
Hello! The error you're encountering:
Parameter 'event' implicitly has an 'any' type.occurs because TypeScript doesn't know the type of the
eventparameter in yourhandleTipoCadastroChangefunction. In TypeScript, especially when thenoImplicitAnycompiler option is enabled (which is common for stricter type checking), all variables and parameters must have an explicit type defined.Here's what's happening:
-
Line with the error:
const handleTipoCadastroChange = (event) => {The
eventparameter doesn't have a type annotation, so TypeScript assigns it an implicitanytype, which it warns about.
To fix this issue, you need to specify the type of the
eventparameter. Since you're using this function as anonChangehandler for theRadioGroupcomponent from@mui/material, the event is of typeReact.ChangeEvent<HTMLInputElement>.Here's how you can modify your code:
-
Import the necessary types from React (if not already present):
import React, { useState, ChangeEvent } from 'react';- Note: If you're only importing types (like
ChangeEvent) and not using any React JSX in this file, you can import React just for the types. However, since you're returning JSX, you should import React.
- Note: If you're only importing types (like
-
Update the
handleTipoCadastroChangefunction to include the type annotation:const handleTipoCadastroChange = (event: React.ChangeEvent<HTMLInputElement>) => { setTipoCadastro(event.target.value); };- Here,
React.ChangeEvent<HTMLInputElement>specifies that the event is a change event originating from an HTML input element.
- Here,
-
Full updated component:
import React, { useState } from 'react'; import "./Clientes.css"; import "../Cadastrar.css"; import ClientesFisica from "./Clientes-Fisica"; import ClientesJuridica from "./Clientes-Juridica"; import { Link } from "react-router-dom"; import { RadioGroup, FormControlLabel, Radio, FormControl } from "@mui/material"; function Cadastrar() { const [tipoCadastro, setTipoCadastro] = useState("fisica"); const handleTipoCadastroChange = (event: React.ChangeEvent<HTMLInputElement>) => { setTipoCadastro(event.target.value); }; return ( <div style={{ textAlign: "center" }}> <FormControl component="fieldset" style={{ marginBottom: "20px" }}> <RadioGroup row value={tipoCadastro} onChange={handleTipoCadastroChange} > <FormControlLabel value="fisica" control={<Radio color="primary" />} label="Pessoa Física" /> <FormControlLabel value="juridica" control={<Radio color="primary" />} label="Pessoa Jurídica" /> </RadioGroup> </FormControl> <div className="Voltar"> <Link to="../Cadastrar">Voltar</Link> </div> <div> {tipoCadastro === "fisica" && <ClientesFisica />} {tipoCadastro === "juridica" && <ClientesJuridica />} </div> </div> ); } export default Cadastrar;
Explanation:
- React.ChangeEvent: This type represents a change event from an HTML input element, which is exactly what the
RadioGroupemits when a selection changes.
By specifying the type of
event, you inform TypeScript about the structure of theeventobject, allowing proper type checking and IntelliSense support in your editor.Alternative Approach:
The
onChangehandler forRadioGroupcomponents from Material-UI sometimes provides the selected value as a second parameter. If you prefer to use this, you can adjust your handler accordingly:-
Update the handler to accept the second parameter
value:const handleTipoCadastroChange = ( event: React.ChangeEvent<HTMLInputElement>, value: string ) => { setTipoCadastro(value); }; -
Use the second parameter
valueinstead ofevent.target.value.
However, in your current usage, since you're accessing
event.target.value, the first method suffices.Final Notes:
- Ensure all types are correctly imported: If you face issues with the
React.ChangeEventtype, make sure React is imported properly in your file. - TypeScript's Strict Mode: The error happens because TypeScript wants you to write safer code by explicitly defining types. This helps catch bugs early and improves code readability.
Let me know if this resolves your issue or if you have any more questions!
-