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

277
Visualizações
Typescipt evaluates conditional types incorrectly

I have a very primitive code that for some reason gives a type error.

type ConfigEntry = {
  name?: string,
  value?: string
}

function parser1(c: ConfigEntry): Record<string, string> {
  const name = c.name;
  const value = c.value;
  return { name: value }; // fine, error since name can be undefined
}

function parser2(c: ConfigEntry): Record<string, string> | undefined {
  const name = c.name;
  const value = c.value;
  if (name === undefined) return undefined;
  else return { name: value }; // compiler still argues that name can be undefined
  // but in any circumstances it is not
}

If I understand TS correctly, compiler should examine conditions and evaluate types accordingly.

Also none of the other conditional expressions known to me work as well.

Example is here

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

0

You are only doing null check for name, while you should be doing it for value instead.

function parser2(c: ConfigEntry): Record<string, string> | undefined {
  const name = c.name; // <--- This line is not needed since we are not doing anything with `name`
  const value = c.value;
  if (value === undefined) return undefined; // <--- Should check `value` here. `name` is not needed because it is unused.
  else return { name: value };
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You are checking that name is undefined. However here

return { name: value };

What you are really using is the value variable. The only thing you need to change is checking that value is undefined:

if (value === undefined) return undefined;
else return { name: value };
about 4 years ago · Juan Pablo Isaza Relatório

0

What lead me to confusion is that because of a typo (name vs [name]) TS was highlighting key, not value, that actually had an error
enter image description here
So the correct code will be fixing the typo and specifying the return value as

Record<string, string | undefined>

function parser4(c: ConfigEntry): Record<string, string | undefined> | undefined {
  const name = c.name;
  const value = c.value;
  return name ? { [name]: value } : undefined;
}

Thank you all for the help.

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