Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda