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

135
Visualizações
Creating plain object with class type definition

Assume we have the following class:

class A {
  private maxValue = 900;
  public minValue = 800;
  someMethod() {}
}

And I want to create a plain object with the class type definition:

const objA: A = {
  minValue: 200,
  maxValue: 2313,
  someMethod() {}
};

And I'm getting this error:

Property 'maxValue' is private in type 'A' but not in type '{ minValue: number; maxValue: number; someMethod(): void; }

The question is: Does it make sense to create objects in this way? or Should I always stick with new A()

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

0

I don't think it's an ideal way to define an object with this type. If you want to define an object like this, use an interface instead of a class. In case of creating new object regarding class A, new A() is a better solution for creating a new object.

about 4 years ago · Juan Pablo Isaza Relatório

0

No, it does not make too much sense. If you check objA instanceof A it will return false, although your code states it would be an instance of class A. And as you can see a plain object can not have private properties whereas a class instance can. So better stick to new A() or use interfaces.

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