Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!