Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
Why doesn't typescript warn about the return type in this function?

For example, I have an onClick handler on a <div> tag. The handler should expect a return type of React.MouseEventHandler<HTMLDivElement> | undefined . But, I still can return a boolean value of false when temp = false. Got confused here why Typescript did not warn me about the return type

import React from 'react';

const Dummy= () => {
  const [temp, settemp] = React.useState(false)
  const [text, settext] = React.useState('Hi')

  const handler : React.MouseEventHandler<HTMLDivElement> | undefined  = () =>{
    return temp && settext('bye')
    //return temp
    
  }
  return (
   <div onClick={handler}> Testing </div>
  );
};

export default Dummy;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're telling typescript that the handler variable is React.MouseEventHandler<HTMLDivElement> | undefined. Note that it is never actually undefined, as it is a const and is therefore only ever a function. You're not telling it what the function's return type is. These are two different things. The return type is either derived from what you return in the function, or you can explicitly declare it after the parameters, like this:

let sum = (x: number, y: number): number => {
    return x + y;
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda