4

Szenario

A method matter returns an object, like return {content, data}

Conflict

The second call of the method (a method from a node module) overwrites previous vars set from return.

import matter from 'gray-matter'

const test = () => {
...
   const { content, data } = matter(source1)
   const { content, data } = matter(source2) // this overwrites previous content, data vars
...
}

Goal / Question

Setting the return values in different named vars, like:

const { content2, data2 } = matter(source2) // like so it leads to an compiling error property content2 does not exists on type [...]

So, how can be the return values assigned to different named vars as they are named in the type?

DrNo
  • 43
  • 4

1 Answers1

5

Just use different variable names:

const { content, data } = matter(source1)
const { content: content2, data: data2 } = matter(source2)

Or don't destructure at all:

const result1 = matter(source1)
const result2 = matter(source2)

Or use an array of objects:

const results = [source1, source2].map(matter);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320