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

290
Visualizações
Function doesn't work when tested with typescript?

I have a strange problem. I have the below function in js:

const removeDuplicates = (inputData) => {
  return [
    ...new Map(inputData.map((data) => [JSON.stringify([data.x, data.y, data.v]), data])).values()
  ];
};

And I'm testing it like so in a jest typescript test:

it('removeDuplicates', () => {
    const expectedObject = [
    { x: 'G', y: 'B', v: '12345' },
    { x: 'A', y: 'L', v: '12345' },
    { x: 'A', y: 'W', v: '123' },
    { x: 'A', y: 'W', v: '1234' }
];
    
    
    const inputData = [
      { x: 'G', y: 'B', v: '12345' },
      { x: 'A', y: 'L', v: '12345' },
      { x: 'A', y: 'L', v: '12345' },
      { x: 'A', y: 'W', v: '123' },
      { x: 'A', y: 'W', v: '1234' },
      { x: 'A', y: 'W', v: '1234' }
    ];
    expect(removeDuplicates(inputData)).toEqual(expectedObject);
  });

When the test is in a js file it passes, when I convert the test file to a ts file it, fails and the function returns nothing! Any ideas?

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

0

Seems like the compiler doesn't like it without the compiler option ''--downlevelIteration'. Did you set this?

Type 'IterableIterator<unknown>' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.ts(2569)

You could use Array.from instead:

type MyThing = { x: string; y: string; v: string };

const removeDuplicates = (inputData: MyThing[]): MyThing[] =>
  Array.from(
    new Map(
      inputData.map((data) => [JSON.stringify([data.x, data.y, data.v]), data])
    ).values()
  );

You should also consider using an external library for such trivial tasks. E.g. Ramda's uniq function should do exactly the same.

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