Estoy tratando de compilar el proyecto y me gustaría implementarlo en vercel, pero obtengo algunos errores de tipo.
El sitio funciona bien en desarrollo (usando yarn dev).
Estas son las dos funciones que estoy usando para cerrar un modal sin propagación de eventos.
//Closes the modal const exitModal = (e) => { closeModal(false) //this part stops the click from propagating if (!e) var e = window.event e.cancelBubble = true if (e.stopPropagation) e.stopPropagation() } const exitAndRefresh = (e) => { exitModal(e) window.location.reload() }El JSX a continuación con la función onClick:
<button className="absolute z-10 top-0 right-0 mt-1 mr-2 p-2 rounded-lg text-white bg-red-500 hover:bg-red-700" onClick={exitModal} > Cancel </button>Error de compilación: (usando la compilación de hilo)
Type error: Subsequent variable declarations must have the same type. Variable 'e' must be of type 'any', but here has type 'Event'. 91 | 92 | //this part stops the click from propagating > 93 | if (!e) var e = window.event | ^ 94 | e.cancelBubble = true 95 | if (e.stopPropagation) e.stopPropagation() 96 | }Intenté hacer esto -
//Closes the modal const exitModal = (e: Event) => { closeModal(false) //this part stops the click from propagating if (!e) var e = window.event e.cancelBubble = true if (e.stopPropagation) e.stopPropagation() } const exitAndRefresh = (e: Event) => { exitModal(e) window.location.reload() }Pero obtuve este error en su lugar:
Type error: Type '(e: Event) => void' is not assignable to type 'MouseEventHandler<HTMLButtonElement>'. Types of parameters 'e' and 'event' are incompatible. Type 'MouseEvent<HTMLButtonElement, MouseEvent>' is missing the following properties from type 'Event': cancelBubble, composed, returnValue, srcElement, and 7 more. 158 | <button 159 | className="absolute z-10 top-0 right-0 mt-1 mr-2 p-2 rounded-lg text-white bg-red-500 hover:bg-red-700" > 160 | onClick={exitModal} | ^ 161 | > 162 | Cancel 163 | </button> error Command failed with exit code 1.Este segmento es un fragmento obsoleto que detiene la propagación (entre navegadores):
//this part stops the click from propagating if (!e) var e = window.event e.cancelBubble = true if (e.stopPropagation) e.stopPropagation()Personalmente, no creo que debamos apoyar a IE indirectamente al incluir un código adicional para él...
Pero de todos modos no necesitas una var allí:
// const exitModal = (e: Event) => { // ^^^^^^^ don't forget to annotate e ??= window.event!; // if e is not there set it to window.event e.cancelBubble = true; e.stopPropagation?.(); // if stopPropagation exists call itEl signo de exclamación allí es para decirle a TypeScript que existe window.event.
Este código utiliza las funciones más recientes de JavaScript: asignación de fusión nula y encadenamiento opcional con llamadas a funciones.
Dado que está utilizando un sistema de compilación implícitamente con NextJS, puede usarlos y no tiene que preocuparse por los navegadores más antiguos.