spread-syntax

Spread syntax ES6 with statement

杀马特。学长 韩版系。学妹 提交于 2019-12-02 11:42:18
I tried to write ternary operator with spread syntax and copy two objects. Is it possible to use ternary operator with spread syntax inside with literal objects? My code works okay, I just want to optimize it. hintStyle: disabled ? {...globalStyles.hint, ...globalStyles.hintDisabled} : globalStyles.hint, I want to write like this: hintStyle: {...globalStyles.hint, {disabled ? ...globalStyles.hintDisabled : {}}}, Spread is not an operator , it's part of the object literal syntax (or at least it will be when the proposal is accepted). You need to write {...globalStyles.hint, ...(disabled ?

Spread syntax returns unexpected object

时光毁灭记忆、已成空白 提交于 2019-12-01 20:40:45
问题 I am using node and i have used . babel-node "start": "nodemon --exec babel-node --presets es2015 index.js" My spread syntax is not working as expected. Here is my code. export const login = async (parentValue, { email, password }) => { try { const user = await User.findOne({ email }); console.log(user); if (!user.authenticateUser(password)) { throw new Error('Wrong password'); } const dummyObject = { ...user }; console.log({ dummyObject }); return { ...user }; } catch (e) { console.log(e);

Spread syntax returns unexpected object

青春壹個敷衍的年華 提交于 2019-12-01 18:52:51
I am using node and i have used . babel-node "start": "nodemon --exec babel-node --presets es2015 index.js" My spread syntax is not working as expected. Here is my code. export const login = async (parentValue, { email, password }) => { try { const user = await User.findOne({ email }); console.log(user); if (!user.authenticateUser(password)) { throw new Error('Wrong password'); } const dummyObject = { ...user }; console.log({ dummyObject }); return { ...user }; } catch (e) { console.log(e); throw new Error(e.message); } }; The line where i have used console.log(user) , it works fine. It

Why is a spread element unsuitable for copying multidimensional arrays?

坚强是说给别人听的谎言 提交于 2019-12-01 06:49:06
From mdn: Spread Syntax Note: Typically the spread operators in ES2015 goes one level deep while copying an array. Therefore, they are unsuitable for copying multidimensional arrays. It's the same case with Object.assign() and Object spread syntax. Look at the example below for a better understanding. var a = [[1], [2], [3]]; var b = [...a]; b.shift().shift(); // 1 // Now array b is: [[2], [3]] What is the point of the above statement? The above code sample works just the same as if you'd copied the array in a to b using the .slice() method. I tried adding another dimension to the array here:

Why is a spread element unsuitable for copying multidimensional arrays?

老子叫甜甜 提交于 2019-12-01 05:54:51
问题 From mdn: Spread Syntax Note: Typically the spread operators in ES2015 goes one level deep while copying an array. Therefore, they are unsuitable for copying multidimensional arrays. It's the same case with Object.assign() and Object spread syntax. Look at the example below for a better understanding. var a = [[1], [2], [3]]; var b = [...a]; b.shift().shift(); // 1 // Now array b is: [[2], [3]] What is the point of the above statement? The above code sample works just the same as if you'd

State as array of objects vs object keyed by id

我们两清 提交于 2019-11-29 18:45:42
In the chapter on Designing the State Shape , the docs suggest to keep your state in an object keyed by ID: Keep every entity in an object stored with an ID as a key, and use IDs to reference it from other entities, or lists. They go on to state Think of the app’s state as a database. I'm working on the state shape for a list of filters, some of which will be open (they're displayed in a popup), or have selected options. When I read "Think of the app’s state as a database," I thought about thinking of them as a JSON response as it would be returned from an API (itself backed by a database). So

Typescript recursive function composition

﹥>﹥吖頭↗ 提交于 2019-11-29 12:30:06
I want to create a function chain, which would be an input of a pipe/flow/compose function. Is this possible without the literal expansion of the types to selected depth, as is this usually handled? See lodash's flow . I want to achieve typecheck of the data flow in the chain. - Argument of a function is result of the previous one - First argument is a template parameter - Last return is a template parameter type Chain<In, Out, Tmp1 = any, Tmp2 = any> = [] | [(arg: In) => Out] | [(arg: In) => Tmp1, (i: Tmp1) => Tmp2, ...Chain<Tmp2, Out>]; The idea is in the draft. This however produces tho

How to add TypeScript types to destructured parameters using spread syntax?

会有一股神秘感。 提交于 2019-11-29 11:06:17
Ignore the fact that this is bad add function. It's a question about using array destructuring with spread syntax in TypeScript. This is what I'm trying const add = ([x,...xs]) => { if (x === undefined) return 0 else return x + add(xs) } console.log(add([1,2,3])) //=> 6 But I have no idea how to add TypeScript types to this. My best guess is to do something like this (most direct translation) const add = (xs: number[]): number => { if (xs[0] === undefined) return 0; else return xs[0] + add(xs.slice(1)); }; console.log(add([1,2,3])); // => 6 Both functions work, but in TypeScript I lose the

PHP Spread Syntax in Array Declaration

大城市里の小女人 提交于 2019-11-29 03:04:19
PHP supports the spread syntax for variadic functions . In JavaScript, you can use the spread syntax to do this : var a = [1, 2]; var b = [...a, 3, 4]; console.log(b); // [1, 2, 3, 4] However, trying to do this in PHP: $a = [1, 2]; $b = [...$a, 3, 4]; var_dump($b);die; Results in this error: Parse error: syntax error, unexpected '...' (T_ELLIPSIS), expecting ']' Is using the spread syntax this way not allowed in PHP? If so, is there an equally-as-elegant way to achieve the same effect? Erald Karakashi The spread operator in the arrays RFC has been implemented in PHP 7.4: $ary = [3, 4, 5];

Using spread operator to update an object value

流过昼夜 提交于 2019-11-28 23:27:57
I have a function which adds a key to incoming object, but I have been told to use spread operator for that, I have been told that I can use the spread operator to create a new object with the same properties and then set isAvailable on it. return new Partner(ServerConfig, capabilities, initialState) } class Partner { constructor (ServerConfig, capabilities, initialState) { initialState.isAvailable = true So I tried something like this but coulndt succeed, can you help me ? and confused, should I use spread operator in this way , return from a function ? newObject = {} // use this inside a