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

103
Visualizações
How to optimize duplicate TS checks if the value may be undefined

Can the following code be simplified for readability, simplicity, or any other way? Specifically, can this be written without checking for undefined on every line?

These ok statements are only some of the many filters that are checked, so this list is large and unruly.

  const aOk = data?.a ? data.a.includes(a) : true
  const bOk = data?.b ? data.b <= b : true
  const cOk = data?.c ? data.c >= c : true
  const dOk = data?.d ? data.d === d : true
  const eOk = data?.e ? data.e === e.toString() : true

  if (
    aOk &&
    bOk &&
    cOk &&
    dOk &&
    eOk
  ) {
    return true
  }

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

0

You could create a helper function such as

const fn = (data) = (key, op, value) =>
  data[key] ? op(data[key], value) : true

Then you can define a set of functions that do the comparisons you need. This might remove repetition, and improve maintainability. You may be able to use something like operations[key](this[key], data[key]) if your variables in data and the scope match up.

const comp = fn(data)

const aOk = comp(‘a’, myIncludes, a)
about 4 years ago · Juan Pablo Isaza Relatório

0

If the shape of validation doesn't change often, I don't see a problem with your function. As another poster mentioned you can instead create a list of functions. Something like this:

type TData = {
  a: number[];
  b: number;
  c: number;
  d: number;
  e: string;
};

const data = {
  a: [3],
  b: 4,
  c: 4,
  d: 1,
  e: "9",
};

const validators: (data:TData, v: { [k in keyof TData]: number }) => {
  [k in keyof TData]: () => boolean;
} = (v) => ({
  a: () => data.a.includes(v.a),
  b: () => data.b <= v.b,
  c: () => data.c >= v.c,
  d: () => data.d === v.d,
  e: () => data.e === v.e.toString(),
});

const all = () => {
  for (const k of Object.values(validators(data, otherData))) {
    if (!k()) return false;
  }
  return true;
};
about 4 years ago · Juan Pablo Isaza Relatório

0

This seems clear and is readable to me:

TS Playground

const allOk = Boolean(
  data?.a ? data.a.includes(a) : true
  && data?.b ? data.b <= b : true
  && data?.c ? data.c >= c : true
  && data?.d ? data.d === d : true
  && data?.e ? data.e === e.toString() : true
);
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