3

With TS imports, I believe I can do this:

import {foo as bar} from 'foo';

with ES6 object destructuring in JS or TypeScript - is there a way to rename an "imported value" in the same way?

For example,

const {longVarName as lvn} = x.bar;
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

3

Use the solution suggested by Jaromanda X:

const {longVarName: lvn} = x.bar;

In fact, you can do more than that:

Multiple variables

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

Default values:

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

Nested objects:

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

For more info, see https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

m1kael
  • 2,801
  • 1
  • 15
  • 14