GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Docs
  • Setup
  • Try it out
  • Videos
  • Blog
  • Donate
  • Team
  • GitHub
  • Guides
  • Config Reference
  • Presets
  • @babel/preset-env
  • ES2026
  • ES2025

This view

  • Original page
  • New address

GeezerVison is showing babeljs.io — @babel/plugin-transform-object-rest-spread · Babel

info

This plugin is included in @babel/preset-env, in ES2018

Example​

Rest Properties​

JavaScript
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
console.log(x); // 1
console.log(y); // 2
console.log(z); // { a: 3, b: 4 }

Spread Properties​

JavaScript
let n = { x, y, ...z };
console.log(n); // { x: 1, y: 2, a: 3, b: 4 }

Installation​

  • npm
  • Yarn
  • pnpm
  • Bun
npm install --save-dev @babel/plugin-transform-object-rest-spread
yarn add --dev @babel/plugin-transform-object-rest-spread
pnpm add --save-dev @babel/plugin-transform-object-rest-spread
bun add --dev @babel/plugin-transform-object-rest-spread

Usage​

With a configuration file (Recommended)​

babel.config.json
{
"plugins": ["@babel/plugin-transform-object-rest-spread"]
}

Via CLI​

Shell
babel --plugins @babel/plugin-transform-object-rest-spread script.js

Via Node API​

JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-object-rest-spread"],
});

Options​

By default, this plugin will produce spec compliant code by using Babel's objectSpread helper.

loose​

boolean, defaults to false.

History
VersionChanges
v8.0.0Deprecated in favor of assumptions

Enabling this option will use Babel's extends helper, which is basically the same as Object.assign (see useBuiltIns below to use it directly).

caution

Consider migrating to the top level setSpreadProperties assumption.

babel.config.json
{
"assumptions": {
"setSpreadProperties": true
}
}

Please keep in mind that even if they're almost equivalent, there's an important difference between spread and Object.assign: spread defines new properties, while Object.assign() sets them, so using this mode might produce unexpected results in some cases.

For detailed information please check out Spread VS. Object.assign and Assigning VS. defining properties.

useBuiltIns​

boolean, defaults to true if the targets support Object.assign, false otherwise.

History
VersionChanges
v7.13.8Default changed to true when the targets support Object.assign

Enabling this option will use Object.assign directly instead of the Babel's extends helper.

Example​

.babelrc

JSON
{
"assumptions": {
"setSpreadProperties": true
},
"plugins": [
["@babel/plugin-transform-object-rest-spread", { "useBuiltIns": true }]
]
}

In

JavaScript
z = { x, ...y };

Out

JavaScript
z = Object.assign({ x }, y);
tip

You can read more about configuring plugin options here

References​

  • Proposal: Object Rest/Spread Properties for ECMAScript
  • Spec
  • Spread VS. Object.assign
  • Assigning VS. defining properties
  • Example
    • Rest Properties
    • Spread Properties
  • Installation
  • Usage
    • With a configuration file (Recommended)
    • Via CLI
    • Via Node API
  • Options
    • loose
    • useBuiltIns
  • References
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.