typescript - 使用 typecirpt 对对象进行部分更新
问题描述
我想要实现的是创建一个实用程序函数,该函数将实例和对象除外,并使用提供的输入字段值更新实例。这是效用函数:
function updateEntity<T, K extends keyof T>(
entity: T,
updateInput: { [key in K]: T[K] },
): void {
return Object.keys(updateInput).forEach((key) => {
entity[key as K] = updateInput[key as K];
});
}
在这里我有一个实例和一个更新界面:
class Animal {
name: string = 'no name';
isMale: boolean = false;
}
type Input = Partial<{
name: string;
}>;
const input: Input = {
name: 'str'
};
const cat = new Animal();
updateEntity(cat, input); // input is an error
我收到以下错误:
Argument of type 'Partial<{ name: string; }>' is not assignable to parameter of type '{ name: string; }'.
Types of property 'name' are incompatible.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.(2345)
我不认为我完全理解错误信息。我的意图是使用提供的输入部分更新原始实例。
输入必须是原始实例的子集(没有额外的字段)。
这是游乐场的链接:link
我错过了什么?
解决方案
使用非空断言。当您将鼠标悬停在updateInput
参数上时,其类型将为,{ [key in K]?: T[K] | undefined }
因为缺少的字段将为undefined
. TypeScript 仍然相信updateInput[key]
can beundefined
而不是T[K]
,但我们很确定updateInput[key]
can only be of type T[K]
。
function updateEntity<T, K extends keyof T>(
entity: T,
updateInput: { [key in K]?: T[K] },
): void {
for (const key in updateInput) {
entity[key] = updateInput[key]!;
}
}
推荐阅读
- jquery - 如何在点击“阅读更多”雪佛龙时显示段落的原始 html()
- kubernetes - 如何禁用 nginx 入口控制器的自动 https 重定向?
- javascript - 线条动画完成时填充 SVG
- java - 如何在 GUI 编辑器中实现文档的版本跟踪
- javascript - 在javascript中一一检查数组对象的所有索引
- azure-data-factory-2 - 将 Rest API 数据复制到 Azure SQL DB 时出现collectionReference 问题
- list - F# 拒绝根据多个约束确定类型
- dart - 如何创建所有类和小部件都可以在 Flutter 中使用的通用数据源
- python - 选择最强的 SIFT 特征进行人脸识别
- javascript - 如何通过在 for 循环中使用类名来获取节点?