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

124
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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