我想IMessage用具有相同类型的新对象更新类型的对象
interface IMessage {
id: string,
text: string,
// and many more attributes
}
const message: IMessage = {
id: "_id",
text: "old text",
// and many more attributes
}
const newMessage: IMessage = {
id: "_id",
text: "new text"
// and many more attributes
}
我期待我的message更新后将具有与 完全相同的值newMessage,但我不能仅仅重新分配message对象,因为它message被声明为const. 这是我到目前为止
const updateMessage = (message: any, newMessage: typeof message): void => {
for (const attr in newMessage) {
if (attr !== "id" && newMessage.hasOwnProperty(attr)) {
message[attr] = newMessage[attr];
}
}
};
此功能正在运行。但是在这个函数中,我必须声明(message: any, newMessage: typeof message),否则 Typescript 会抛出警告Element implicitly has an 'any' type because type 'IMessage' has no index signature。我怎样才能让这个函数只接受 type 的参数IMessage?
慕标5832272
慕慕森
相关分类