打字稿进口与ES6对象解构

打字稿进口与ES6对象解构

问题描述:

有了TS进口,我相信我能做到这一点:打字稿进口与ES6对象解构

import {foo as bar} from 'foo'; 

与JS或打字稿ES6对象解构 - 是有没有办法重命名的“导入值”同样的方式?

例如,

const {longVarName as lvn} = x.bar; 
+5

'常量{longVarName:LVN} = x.bar;'?会导致一个名为lvn的const值为x.bar.longVarName –

使用由Jaromanda X建议的解决方案:

const {longVarName: lvn} = x.bar; 

事实上,你可以做更多的事:

多个变量

var {p: foo, q: bar} = o; 

默认值为:

var {a = 10, b = 5} = {a: 3}; 

嵌套对象:

const x = {a: {b: {c: 10}}}; 
const {a: {b: {c: ten}}} = x; 
// ten === 10 

有关详情,请参阅https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

+0

这个ES6的权利?不是打字稿? –

+0

是的,这是ES6,是正确的。 – m1kael

+1

我可以确认它与TypeScript一起工作,这很有意义,因为TS紧跟ES6。 –