Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

289
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!