From 7aecafe532d53748273b77ae13346a2191894750 Mon Sep 17 00:00:00 2001 From: Dumitru Deveatii Date: Tue, 26 Apr 2022 10:42:16 +0300 Subject: [PATCH] add example with spread operator --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 3a99f8e..07874ec 100644 --- a/README.md +++ b/README.md @@ -622,6 +622,24 @@ function createMenu(config: MenuConfig) { createMenu({ body: 'Bar' }); ``` +Or, you could use the spread operator: + +```ts +function createMenu(config: MenuConfig) { + const menuConfig = { + title: 'Foo', + body: 'Bar', + buttonText: 'Baz', + cancellable: true, + ...config, + }; + + // ... +} +``` +The spread operator and `Object.assign()` are very similar. +The main difference is that spreading defines new properties, while `Object.assign()` sets them. More detailed, the difference is explained in [this](https://stackoverflow.com/questions/32925460/object-spread-vs-object-assign) thread. + Alternatively, you can use destructuring with default values: ```ts