question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Object spread operator not supported?

See original GitHub issue

Hi, I’m having trouble using object spread operators. I’m using gatsby together with typescript. Typescript does not complain. I must be related to gatsby’s build process? (Module build failed: SyntaxError: Unexpected token).

Here’s the actual code it complains about

data.push({ key: doc.id, ...doc.data(), })

Using Object.assign() works… but meeeww… 👎

data.push(Object.assign(doc.data(), { key: doc.id }))

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
clarkdavecommented, Jan 9, 2018

@wedelgaard

I’ve figured out why this happens, and have a solution. The gatsby typescript plugin runs the ts-loader on .tsx? files, followed by babel with the extract-graphql plugin to look for and transform graphql literals.

The invocation of the babel-loader here doesn’t explicitly enable any babel plugins, so babel is unable to parse object spread (and presumably decorators too) because tsc passes those through as they are.

Easy fix though! Just make sure you have a .babelrc in your project root with the env preset and the transform-object-rest-spread plugin:

{
  "presets": ["env"],
  "plugins": ["transform-object-rest-spread"]
}

Once this is done, the invocation of babel that runs after TypeScript will be able to parse the spread syntax and, in combination with the env preset, will transpile everything down so it can be safely bundled during a build too. You can configure the env preset with the same options as Gatsby.

Babel 7 supports native parsing of TypeScript, so when that’s released and incorporated into Gatsby it may be easier to use that instead of the current double transpilation approach.

2reactions
wedelgaardcommented, Nov 21, 2017

@alampros just managed to figure out what was wrong. Using the optional configuration stated at https://www.gatsbyjs.org/packages/gatsby-plugin-typescript/ seems to do the job just fine.

plugins: [ { resolve: 'gatsby-plugin-typescript', options: { transpileOnly: true, // default compilerOptions: { target: esnext, experimentalDecorators: true, jsx: react, }, // default } }, ]

Instead of using

plugins: [ gatsby-plugin-typescript, ]

Thanks for having a look at this 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript spread operator for object is not working? Is this ...
The reason behind asking this question is, I saw the use of spread operator using an object in the below line of code...
Read more >
Spread syntax (...) - JavaScript - MDN Web Docs - Mozilla
In an object literal, the spread syntax enumerates the properties of an object and adds the key-value pairs to the object being created....
Read more >
"spread operator" | Can I use... Support tables for ... - CanIUse
"Can I use" provides up-to-date browser support tables for support of front-end web ... JavaScript operator: Object initializer: Spread properties.
Read more >
How to Use the Spread Operator (…) in JavaScript - Medium
That means that changes to the original array will not affect the copied array, ... The spread operator … is useful for working...
Read more >
rest-spread-spacing - ESLint - Pluggable JavaScript Linter
When using the default "never" option, whitespace is not allowed between spread operators and their expressions. rest-spread-spacing: ["error"].
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found