destructuring

Typed function parameters using destructuring and rest in TypeScript

社会主义新天地 提交于 2020-08-22 04:44:01
问题 I have a function: export default ({ input: { name, onChange, value, ...restInput }, meta, ...rest }) => ( ... ); Given that 'name' is a string, 'onChange' is a function, 'value' is a string, 'meta' is an object, how can I add types to those parameters? My best guess would be like so: export default ({ input: { (name: String), (onChange: function), (value: String), ...restInput }, (meta: Object), ...rest }) => ( ... ); But it seems to have syntax errors. And even more I have no idea how to

Typed function parameters using destructuring and rest in TypeScript

走远了吗. 提交于 2020-08-22 04:43:07
问题 I have a function: export default ({ input: { name, onChange, value, ...restInput }, meta, ...rest }) => ( ... ); Given that 'name' is a string, 'onChange' is a function, 'value' is a string, 'meta' is an object, how can I add types to those parameters? My best guess would be like so: export default ({ input: { (name: String), (onChange: function), (value: String), ...restInput }, (meta: Object), ...rest }) => ( ... ); But it seems to have syntax errors. And even more I have no idea how to

ES6 change a value while destructing

邮差的信 提交于 2020-08-20 07:45:14
问题 Lets say I have an object const obj = { width: 100, height: 200 } I wish to pass that object to a method myFunc( obj ); Inside that method I wish to pull out the height, but at the same time subtract a value. I only wish to do this once and after that it will never change. Doing the following will get me the correct height I want of say 150. let {height: localHeight} = obj; localHeight = localHeight - 50; How do I do the above in a single line ? Something like this - but I know this doesn't

ES6 change a value while destructing

生来就可爱ヽ(ⅴ<●) 提交于 2020-08-20 07:45:05
问题 Lets say I have an object const obj = { width: 100, height: 200 } I wish to pass that object to a method myFunc( obj ); Inside that method I wish to pull out the height, but at the same time subtract a value. I only wish to do this once and after that it will never change. Doing the following will get me the correct height I want of say 150. let {height: localHeight} = obj; localHeight = localHeight - 50; How do I do the above in a single line ? Something like this - but I know this doesn't

Destructuring nested objects as function parameters

爷,独闯天下 提交于 2020-07-29 06:32:56
问题 In ES6 we can do: let myFunc = ({name}) => { console.log(name) } myFunc({name:'fred'}) // => logs 'fred' But how do I do it for nested properties like this: myFunc({event:{target:{name:'fred'}}}) // => I want it to log 'fred' What should myFunc look like so that it logs 'fred'? I cannot change the object passed in. I wish to use destructuring to achieve this or some other suitable ES6 approach. 回答1: You can simply do like this: const myFunc = ({event: {target: {name}}}) => { console.log(name)

Destructuring state/props in React

[亡魂溺海] 提交于 2020-07-17 12:22:31
问题 I'm learning React and I have Eslint installed in my project. It started giving me errors like Use callback in setState when referencing the previous state. (react/no-access-state-in-setstate) Must use destructuring state assignment (react/destructuring-assignment) I tried to look this up but couldn't understand properly. Can someone point me in the right direction with this? Here is my code that throws the errors: class LoginForm extends React.Component { state = { data: { email: "",

Destruct using titles

╄→гoц情女王★ 提交于 2020-06-15 07:27:37
问题 I have answered a few questions using destructing, I just want to take this one to the next level I want to not use reduce in this example but pure destructing if at all possible So the data's first row contains the attribute names of the object, How can I use that to be DRY i.e. I hoped for const obj = data.slice(1).map((titles) => ({ titles }) ) or similar So this works, but I miss one more step: const data = [ ["fruits","frozen","fresh","rotten"], ["apples",884,494,494], ["oranges",4848

Java object destructuring

≡放荡痞女 提交于 2020-05-29 02:25:26
问题 In javascript there is object destructuring so we can break down objects and just use the end key if the intermidiate objects are resused multiple times. e.g) const person = { firstName: "Bob", lastName: "Marley", city: "Space" } So instead of calling person.<> to get each value we can destructure it like this console.log(person.firstName) console.log(person.lastName) console.log(person.city) Destructured: const { firstName, lastName, city } = person; And call like this: console.log(firstName

ES6: destructuring an object with symbols as keys

六眼飞鱼酱① 提交于 2020-02-24 10:08:15
问题 I have an object that contains symbols as keys. How do I do destructuring assignment in this case? let symbol = Symbol() let obj = {[symbol]: ''} let { /* how do I create a variable here, that holds the value of [symbol] property? */ } = obj I need to know if this possible, I do know the obvious and simple workarounds, but that's not what I am asking. UPD. Funny enough I knew how to do that but typescript produced errors, and I thought I did something wrong in JS. Here's a fix for typescript

ES6: destructuring an object with symbols as keys

懵懂的女人 提交于 2020-02-24 10:08:13
问题 I have an object that contains symbols as keys. How do I do destructuring assignment in this case? let symbol = Symbol() let obj = {[symbol]: ''} let { /* how do I create a variable here, that holds the value of [symbol] property? */ } = obj I need to know if this possible, I do know the obvious and simple workarounds, but that's not what I am asking. UPD. Funny enough I knew how to do that but typescript produced errors, and I thought I did something wrong in JS. Here's a fix for typescript