vini
vini

Reputation: 4742

Update new values to existing interface typescript

 const localBook = await getBook(bookId);

  const bookRemote: IBook = {
    business_name: bookName,
    business_image: businessImage,
    business_owner_name: businessOwnerName,
    updated_at: String(getCurrentTime()),
    updated_by_user: user.user_id,
    updated_by_device: deviceInfo.device_id,
    book_name: bookName,
  };

  const book = {
    ...localBook,
    bookRemote,
  };

  try {
    // add in local DB
    await updateBookDB(book);
  } catch (e) {
    // catch local DB error and reject current promise
    return Promise.reject(e);
  }

I want to update my property values in an interface typescript however it gives a missing properties error. though I am appending the localBook here.

enter image description here

My Question:

The localBook contains all the interface properties and values, I need to add new ones for the properties that I am passing via the bookRemote object how do I achieve that?

Upvotes: 1

Views: 1613

Answers (2)

tmhao2005
tmhao2005

Reputation: 17514

I'm guessing here is the thing you need:

const bookRemote = {
  business_name: bookName,
  business_image: businessImage,
  business_owner_name: businessOwnerName,
  updated_at: String(getCurrentTime()),
  updated_by_user: user.user_id,
  updated_by_device: deviceInfo.device_id,
  book_name: bookName,
} as IBookValue; // you can take advantage by using IBookValue which requires in your `updateBookDB` method too

const book = {
  ...localBook,
  ...bookRemote, // spread your object
};

Upvotes: 2

Akshay Sethia
Akshay Sethia

Reputation: 37

Okay, according to what i have inferred from the above code is that, you are not able to edit the localBook property in the constant books. SO, according to me you ave to just make it to localBook[]. I think this should help in your case.

Upvotes: 0

Related Questions