Object spread operator not supported?
See original GitHub issueHi, 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:
- Created 6 years ago
- Comments:11 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@wedelgaard
I’ve figured out why this happens, and have a solution. The gatsby typescript plugin runs the
ts-loader
on.tsx?
files, followed bybabel
with theextract-graphql
plugin to look for and transformgraphql
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 theenv
preset and thetransform-object-rest-spread
plugin:Once this is done, the invocation of
babel
that runs after TypeScript will be able to parse the spread syntax and, in combination with theenv
preset, will transpile everything down so it can be safely bundled during a build too. You can configure theenv
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.
@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 😉