将新值更新为现有接口打字稿
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);
}
我想在接口打字稿中更新我的 属性 值,但是它给出了缺少属性的错误。虽然我在这里附加了 localBook。
我的问题:
localBook 包含所有接口属性和值,我需要为通过 bookRemote 对象传递的属性添加新的属性和值,我该如何实现?
好的,根据我从上面的代码推断的是,您不能编辑常量书籍中的localBook 属性。所以,根据我的说法,你必须进入 localBook[]。我认为这对您的情况应该有所帮助。
我猜这是您需要的东西:
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
};
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);
}
我想在接口打字稿中更新我的 属性 值,但是它给出了缺少属性的错误。虽然我在这里附加了 localBook。
我的问题:
localBook 包含所有接口属性和值,我需要为通过 bookRemote 对象传递的属性添加新的属性和值,我该如何实现?
好的,根据我从上面的代码推断的是,您不能编辑常量书籍中的localBook 属性。所以,根据我的说法,你必须进入 localBook[]。我认为这对您的情况应该有所帮助。
我猜这是您需要的东西:
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
};