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

119
Visualizações
Assign null check to reused const in upper scope of function without non-null assertion

Take this code snippet example:

const [interval, setInterval] = useState<PartialInterval | null>(null);

const handleDaySelect = (day: DateData) => {
  const isStartSelected = interval?.start;
  const isEndSelected = interval?.end;

  if (!isStartSelected) {
    setInterval({ start: parseDayToDate(day.dateString) });
    return;
  }

  if (isStartSelected && !isEndSelected) {
    const isSelectedEndBeforeSelectedStart = isBefore(
      parseDayToDate(day.dateString),
      interval.start!,
    );
    
    if (isSelectedEndBeforeSelectedStart) {
      setInterval({
        start: parseDayToDate(day.dateString),
        end: interval.start,
      });
      return;
    }

    setInterval({ ...interval, end: parseDayToDate(day.dateString) });
    return;
  }

  if (isStartSelected && isEndSelected) {
    setInterval({ start: parseDayToDate(day.dateString) });
  }
};

Where the null checks for the interval are assigned as a const value at the beginning of the function. In this case, I want to use no-non-null-assertion so I don't have to write each declaration like so interval.start! is this actually achievable in typescript without explicitly doing the check at each if statement? if(interval?.start) and can continue to use the already defined check of isStartSelected.

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

A handy way will be to use assertive functions to assert and handle the interval is not nullish and upon successful assertion it returns the interval object.

Typescript will never complain about its being null because it is returned from the function.

about 4 years ago · Juan Pablo Isaza Relatório

0

So the answer here:

Use the assigned const variable you've given at the top of the scope.

So instead of writing the variable isStartSelected and using it as a bool check for undefined, you can assign the variable a better name startInterval and then ts will let you use the variable you've assigned as an undefined check as well as able to parse and use it later in the function.

Simple case of overcomplicating the problem

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