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

171
Visualizações
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 Respostas
Responde à pergunta

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