Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Error de mecanografiado: la variable 'resWithStatus' se usa antes de ser asignada. ts(2454)
for (const item of filesForUpload) { let resWithStatus: IResponseType; try { const res = await uploadFile(item); resWithStatus = { id: res?.id, name: res?.name, link: res?.link, ext: res?.ext, status: "SUCCESS", }; } catch (err) { resWithStatus = { id: uuidv4(), name: item.name, link: URL.createObjectURL(item), status: "FAILED", }; console.log(err); } finally { const indexInUploadedImageState = newFileArray.findIndex((imageItem) => { if ( resWithStatus.status === "FAILED" && imageItem.name === resWithStatus.name ) return true; if ( resWithStatus.status === "SUCCESS" && getFileNameWithoutExt(imageItem.name) === resWithStatus.name && getFileExt(imageItem.name) === resWithStatus.ext ) return true; return false; }); console.log(resWithStatus); // Error occurs here } console.log(resWithStatus); }

Así que tengo este fragmento de código escrito en TS, he declarado una variable con la palabra clave let con el tipo dentro del cuerpo del ciclo, pero todavía obtengo el Error : la Variable is used before it is assigned . Hasta donde sé, ¿no debería estar disponible esta variable resWithStatus en todos los bloques de try, catch y finalmente, ya que se declara fuera de ellos y se usa dentro de ellos? Soy bastante nuevo en TS, pero entiendo que no puede hacer referencia a la variable externa resWithStatus y no entiendo por qué.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Tienes razón sobre try , catch y finally tener acceso a la variable resWithStatus . Pero el error dice que se usa resWithStatus antes de asignarlo. Y creo que en este caso, dado que resWithStatus no está inicializado (let finally let resWithStatus: IResponseType; ), TS no puede garantizar que tenga un valor cuando se lea en su bloque final. creo que algo como

 let resWithStatus: IResponseType = {/*initialize resWithStatus*/};

debería arreglarlo

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!