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

147
Visualizações
Exposing a custom getter in a Response DTO does not work

I've tried to define a custom getter like this:

import { Expose } from 'class-transformer';

export class MyDTOResponse {
  @Expose()
  id: string;

  @Expose()
  name: string;

  @Expose()
  get thisIsATest(): string {
    return 'yolo';
  }
}

This is how I transform it:

plainToClass(MyDTOResponse, MyRawDataObject, {
  excludeExtraneousValues: true,
});

As described in the documentation here: https://github.com/typestack/class-transformer#exposing-getters-and-method-return-values

However, the response is only:

{
  "id": "f8c213c7-5853-4d01-b424-cb0349a6c580",
  "name": "Clean the kitchen!"
}

I am not sure what I am doing wrong exactly, but I am missing the "thisIsATest" property.

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

0

plainToClass will return an instance. If you console.log this instance, you can't see the "thisIsATest" property. You can try the following code:

const res = plainToClass(MyDTOResponse, MyRawDataObject, {
  excludeExtraneousValues: true,
});

console.log(res.thisIsATest); // yolo

console.log(instanceToPlain(res))
// {
//   id: 'f8c213c7-5853-4d01-b424-cb0349a6c580',
//   name: 'Clean the kitchen!',
//   thisIsATest: 'yolo'
// }

Btw, plainToClass is deprecated, use the plainToInstance method instead.

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