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

151
Vistas
Destruct and assign in javascript/typescript

I have the following code

const myVariable: number | undefined;

if (someCondition) {
     const { someAttribute, anotherAttribute } = someFunction(); //here I want to assign directly that myVariable to someAttribute. 
     doAnotherTask(anotherAttribute);
  
}

doSomethingWithVariable(myVariable);

How can I destruct someFunction to get someAttribute and directly assign myVariable to it.

const {someAttribute: myVariable} wouldnt work here, since myVariable is defined outside the scope of the condition.

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

0

Just drop the const, since you're not creating a new variable. And myVariable can't be a const since you need to assign a value to it later.

Due to how the JavaScript language works, you also need parentheses around the destructuring statement.

let myVariable: number | undefined;

if (someCondition) {
     ({ someAttribute: myVariable } = someFunction());
}

doSomethingWithVariable(myVariable);
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