Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
ES6 - How to import and alias a named export's member?

Say I have a package that exports a User type:

export const User: {
  name: string;
};

And I want to use the name property aliased as username in my code. In normal JavaScript I'm able to do something like this:

const { User: { name: username } } = require('user.js');

/* do something with `username` ... */

I want to convert the file I'm working with into TypeScript and so I'll use ES6 module imports. How can I achieve the same as above using import instead of require?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can't destructure objects in static import statements, but you can rename them using the as keyword:

import {User as USER} from './user.js';

If you are okay with using a dynamic import, you can destructure:

const {User: {name: username}} = await import('./user.js');

There are pros and cons to both static and dynamic importing methods which you can read about in the links I shared.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda