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

401
Visualizações
Cannot assign 'import class' because its an import - Javascript

I have been initializing some constant classes as

    export class A {
       foo: string,
       b: B
    }

    export class B { 
       bar: number
    }

And then importing them into another class and initializing them in another file. However, if I have issues using class B when I want to initialize class A.

Example:

    import { A, B } from '../models/class';
    export const myVariable: A = {
       foo: 'FOO',
       b: B = {
          bar: 5
       }
    }

the first assignment is correct. However, the B class inside A will give me the error - Cannot assign to 'B' because it is an import.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This isn't how you assign a value to a property in an object literal:

{
  foo: 'FOO',
  b: B = {
    bar: 5
  }
}

Basically you're trying to assign an object to B itself instead of to the b property on your object, and then trying to assign the result of that assignment to the b property. Which doesn't really make sense.

You're probably getting confused over the usages of the : character. When declaring a variable in TypeScript, it's used to indicate the type. When defining an object literal in JavaScript (and therefore also TypeScript), it's used to define a value for an object's property.

This is how you define an object literal:

{
  foo: 'FOO',
  b: {
    bar: 5
  }
}
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