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

375
Vistas
How to convert an object of one class into an object of another class in typescript ReactJs?

I have two interfaces defined like this:

export interface PostView{
    postName: string;
    postTime: Time;
    message: string | null;
    replies: { [P in AccountType]?: Reply[][] };
}

export interface Post{
    postName: string;
    postTime: Time;
    message: string | null;
    replies: { [P in AccountType]?: Reply[] };
}

You can see that both the interface definitions are same except for the replies field.

Now, in a .tsx file, I'm getting an object of PostView. I want to call another function which requires Post object. Also, for the replies field, I have to sent empty object to the function.

This is how I have currently.

  const postViewObject: any = props.myPostView;
  postViewObject.replies= {};
  const request: Post = postViewObject;
  functionToBeCalled(request);

You can see that I'm assigning PostView object to a variable of type any which I don't want to do. And then I'm updating replies field to empty object and casting it to Post object.

Is there a better way to write this code?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could create a new post object using the spread operator:

const request: Post = {
    ...props.myPostView,
    replies: {}
}
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