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

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

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