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 对象传递的属性添加新属性和值,我该如何实现?
解决方案
我猜这是你需要的东西:
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
};
推荐阅读
- android - 我将如何在 android 的 TabLayout 中为需要登录的不同用户设置 viewpager 选项?
- javascript - onclick 道具不适用于 Material UI?
- uncrustify - 打开c评论后取消空间
- javascript - foreach 循环中的异步
- kubernetes - 如何在 ksonnet/jsonnet 中写入多行值
- java - Java 进程控制台输出
- html - 在复杂的 HTML 结构中选择带有 CSS 的元素
- mysql - 查询结果需要时间加载
- c++ - C++ winapi 从共享内存读取到 std::string
- angular - 使用类型=数字的 Angular Dart 材料输入时出现问题