Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda