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

118
Vistas
Variable used before being assigned

I have the typescript error saying: Variable 'trait' is used before being assigned.

Thats down at pickedTraits.push()

I dont get it, i declared trait and assigned it on the begin of the loop, why does typescript complain about?

It works when i put it outside the while loop, but i need it inside of it

  layers.map((layer) => {
      let stop = true
      let trait: TraitInterface
      while (stop) {
        trait = utilsService.pickRandomTrait(layer.traits)
        let reachedMaxmimum = alreadyPicked.findIndex(
          ({ traitId, appeared }) =>
            trait._id.equals(traitId) && (appeared ?? 0) >= trait.showUp
        )
        if (reachedMaxmimum !== -1) {
          layer.traits = layer.traits.filter(
            ({ _id }) => !_id.equals(alreadyPicked[reachedMaxmimum].traitId)
          )
        } else {
          alreadyPicked.push({
            traitId: trait._id,
            appeared: 1,
          })
          stop = false
          continue
        }
      }

      pickedTraits.push({
        name: trait.name,
        rarity: trait.rarity,
        layerName: layer.name,
      })
      return drawingService.addLayer(
        context,
        trait.src,
        size.height,
        size.width
      )
    })
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I have fixed the problem with the ! non-null assertion operator

let trait!: TraitInterface

Now it works

Here is the answer: In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?

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