Amit Erandole
Amit Erandole

Reputation: 12281

Why isn't my spread syntax working in node v7.0.0?

I am trying to use the Object Spread syntax like so:

let credentialsWithAuth =  { ...credentials, type: 'basic' }

Where credentials is an object with username and password as keys and values. But this blows up with SyntaxError: Unexpected token ...

So do I have to setup node with babel for this to work? I thought native support was now built in.

http://node.green/

Can't I use this without Object.assign etc?

Could someone please clarify?

Upvotes: 3

Views: 1856

Answers (2)

Mohamed Sohail
Mohamed Sohail

Reputation: 1867

Yes, it is only supported in node_8x and above. However the correct equivalent using Object.assign() (That does not overwrite the source object) is:

let credentials = { username : 'test', password: 'test' }
let credentialsWithAuth = Object.assign({}, { type: 'basic' }, credentials)

console.log(credentialsWithAuth)

Upvotes: 0

Bartosz Gościński
Bartosz Gościński

Reputation: 1580

Spread syntax which is available in node 7.0 doesn't handle spreading properties of an object. What you're looking for is object spread syntax which is currently on stage 3 of TC39 Process. You can find more info about the process in the process document and info about proposal in its repository.

Upvotes: 3

Related Questions