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

169
Views
Creating named parameter to base class in typescript and javascript

I have created a base model class with is a model for the incoming data, I will be modeling the data inside of a class for some external reasons. Although I ran into a problem with is defining the incoming data into this model class.

This is my class

export class EntityBasic {
  constructor(
    public id: string,
    public title: string,
    public urn?: string,
    public risk_level?: string,
    public be_aware?: string,
    public body?: string,
    public published_at?: string,
    public created_at?: string,
    public updated_at?: string,
    public description?: string,
    public notes?: string,
  ) {}}
}

How I define the content inside of it in another page:

public getEntity(setEntity) {
    return new EntityBasic(
      setEntity.id,
      setEntity.title,
      setEntity?.urn,
      setEntity?.risk_level,
      setEntity?.be_aware,
      setEntity?.body,
      setEntity?.published_at,
      setEntity?.created_at,
      setEntity?.updated_at,
      setEntity?.report?.summary || '',
      setEntity?.report?.metadata?.source_notes,
      setEntity?.report?.metadata?.notes,
    );
  }

But defining the data like so, gave me problems, since some times there won't be a urn, or a risk_level for example, and the data will be messed up inside the class.
I would like a way to define like so (This didnt work):

public getEntity(setEntity) {
    return new EntityBasic(
      id = setEntity.id,
      title = setEntity.title,
      urn = setEntity?.urn,
      risk_level = setEntity?.risk_level,
    )
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I believe wrap all parameters into one object is the best solution.

  1. Wrap them as interface
interface EntityBasicParameters {
    id: string;
    title: string;
    urn?: string;
    risk_level?: string;
    be_aware?: string;
    body?: string;
    published_at?: string;
    created_at?: string;
    updated_at?: string;
    description?: string;
    notes?: string;
}
  1. Accept only this object as constructor parameter
export class EntityBasic {
  constructor(params: EntityBasicParameters) {}}
}
  1. Now you can pass existent data only
public getEntity(setEntity) {
    return new EntityBasic({
      id: setEntity.id,
      title: setEntity.title,
      urn: setEntity?.urn,
      risk_level: setEntity?.risk_level,
    })
}

or just

public getEntity(setEntity) {
    return new EntityBasic(setEntity)
}
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!