admin管理员组文章数量:1396820
I tried to destructuring assignment with interface, but cannot write like this.
interface TYPE {
id?: number;
type?: string;
}
const e = {
'id': 123,
'type': 'type_x',
'other': 'other_x'
}
const {...foo}: {foo: TYPE} = e;
console.log(foo.id, foo.type) // expected: 123, 'type_x'
I tried to destructuring assignment with interface, but cannot write like this.
interface TYPE {
id?: number;
type?: string;
}
const e = {
'id': 123,
'type': 'type_x',
'other': 'other_x'
}
const {...foo}: {foo: TYPE} = e;
console.log(foo.id, foo.type) // expected: 123, 'type_x'
Share
Improve this question
edited Aug 4, 2019 at 6:48
Jack Bashford
44.2k11 gold badges55 silver badges82 bronze badges
asked Aug 4, 2019 at 6:27
wgf4242wgf4242
8313 gold badges12 silver badges23 bronze badges
2
-
3
What do you expect
const {...foo}: {foo: TYPE} = e;
to do exactly? – Alex Wayne Commented Aug 4, 2019 at 6:53 - const {...foo, ...rest}: {foo: TYPE, rest: any} = e,; to desctruct assign TYPE, and rest propterties, but not write like this. – wgf4242 Commented Aug 4, 2019 at 13:20
1 Answer
Reset to default 5Just declare the type on the variable, without that weird object notation:
const { ...foo }: TYPE = e;
That is a weird way to make a copy of an object however - it's usually done like so:
const foo: TYPE = { ...e };
本文标签: javascripttypescript destructuring assignment with interfaceStack Overflow
版权声明:本文标题:javascript - typescript destructuring assignment with interface - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744145848a2592832.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论