spread-syntax

Type safe merge of object literals in typescript

一个人想着一个人 提交于 2020-01-25 10:14:00
问题 I want to merge two typescript objects (using object spread): var one = { a: 1 } var two = { a: 2, b: 3 } var m = {...one, ...two} // problem as property `a` is overwritten I want to use the type system to ensure none of the properties in the second object overwrite any properties in the first. I am not sure why the following solution does not work: type UniqueObject<T extends {[K in keyof U]?: any}, U> = {[K in keyof U]: T[K] extends U[K] ? never : U[K]} var one = { a: 1 } var two1 = { a: 2,

Type safe merge of index signature object types in typescript

情到浓时终转凉″ 提交于 2020-01-24 20:00:05
问题 This question and answer covers object literals but the answer does not work when using index signature object types. e.g: type UniqueObject<T, U> = { [K in keyof U]: K extends keyof T ? never : U[K] } export function mergeUnique <T, U, V> ( a: T, b?: UniqueObject<T, U>, c?: UniqueObject<T & U, V>, ) { return { ...a, ...b, ...c, } } type Obj = { [index: string]: number | undefined } const a: Obj = { a: undefined } const b: Obj = { b: 3 } // should all pass const res01 = mergeUnique({ a:

setting up the babel plugin for spread operator correctly

*爱你&永不变心* 提交于 2020-01-16 18:57:09
问题 Attempting to use babel-plugin-transform-es2015-spread in my project. Installed the module. npm install --save-dev babel-plugin-transform-es2015-spread .babelrc looks like. { "presets": [ ["env", { "include": ["babel-plugin-transform-es2015-spread"] }] ] } Registering babel in my main.js // babel require('babel-core/register') require('babel-polyfill') But the below code snippet still throws (Unexpected token) error return { ...state, hoverTrend: action.trend, } 回答1: The plugin babel-plugin

setting up the babel plugin for spread operator correctly

天大地大妈咪最大 提交于 2020-01-16 18:55:53
问题 Attempting to use babel-plugin-transform-es2015-spread in my project. Installed the module. npm install --save-dev babel-plugin-transform-es2015-spread .babelrc looks like. { "presets": [ ["env", { "include": ["babel-plugin-transform-es2015-spread"] }] ] } Registering babel in my main.js // babel require('babel-core/register') require('babel-polyfill') But the below code snippet still throws (Unexpected token) error return { ...state, hoverTrend: action.trend, } 回答1: The plugin babel-plugin

Redux: Using Spread Operator on 2D array

廉价感情. 提交于 2020-01-15 08:49:10
问题 New to React.js, I am having hard time using the spread operator in my reducers to update my state that has an 2D-array property. For instance initial state is so: let initialState = { grid: new Array(5).fill(new Array(5).fill(0)), player: { coords: [2,3], health: 100 } } After binding the action, lets say the payload goes to PRESS_LEFT case PRESS_LEFT: { let oldCoords = [state.player.coords[0], state.player.coords[1]]; let newCoords = [state.player.coords[0], state.player.coords[1]-1]; let

Spread Operator not working for Redux/ES6 based sample

纵然是瞬间 提交于 2020-01-13 09:44:08
问题 I am trying to understand Redux online tutorials that are posted by Dan Abramov. At present I am on the following sample: Reducer composition with Arrays Here is my practice code following the above sample: // Individual TODO Reducer const todoReducer = (state, action) => { switch(action.type) { case 'ADD_TODO': return { id: action.id, text: action.text, completed: false }; case 'TOGGLE_TODO': if (state.id != action.id) return state; // This not working /* return { ...state, completed: !state

Replace array entry with spread syntax in one line of code?

我只是一个虾纸丫 提交于 2020-01-12 14:04:39
问题 I'm replacing an item in a react state array by using the ... spread syntax. This works: let newImages = [...this.state.images] newImages[4] = updatedImage this.setState({images:newImages}) Would it be possible to do this in one line of code? Something like this? (this doesn't work obviously...) this.setState({images: [...this.state.images, [4]:updatedImage}) 回答1: use Array.slice this.setState({images: [...this.state.images.slice(0, 3), updatedImage, ...this.state.images.slice(4)]}) 回答2:

Replace array entry with spread syntax in one line of code?

ε祈祈猫儿з 提交于 2020-01-12 14:03:13
问题 I'm replacing an item in a react state array by using the ... spread syntax. This works: let newImages = [...this.state.images] newImages[4] = updatedImage this.setState({images:newImages}) Would it be possible to do this in one line of code? Something like this? (this doesn't work obviously...) this.setState({images: [...this.state.images, [4]:updatedImage}) 回答1: use Array.slice this.setState({images: [...this.state.images.slice(0, 3), updatedImage, ...this.state.images.slice(4)]}) 回答2:

Spread syntax ES6 with statement

久未见 提交于 2019-12-31 07:44:08
问题 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 : {}}}, 回答1: Spread is not an operator, it's part of the object literal syntax (or at

Alternatives of spread syntax

依然范特西╮ 提交于 2019-12-24 11:04:06
问题 I see several uses of spread syntax in a code. For example: function tree2table(tree) { var children = tree["children"]; if (children === undefined) return []; var result = []; for (var i = 0; i < children.length; i++) { var child = children[i]; var link = [child["name"], tree["name"], child["size"]]; result.push(link); result.push(...tree2table(child)) } return result } However, spread syntax is not supported in IE. Does anyone know what is the best way to change result.push(...tree2table