我刚开始使用TS,现在我正在运行一个测试,使用的是对react/类型记录的玩笑,并且我从类型脚本中得到了这个错误,上面写着The operand of a 'delete' operator must be optional.
,我无法找到停止TS的方法来停止喊我已经尝试过了
interface Thing {
prop: string | undefined;
}
但是它不适用于jest,测试也因此失败,请注意,这是我的代码片段
it('mergeFirestoreData', () => {
const returnValue = mergeFirestoreData(`/${collectionNames.PERMITS}/id`, new PermitCore(), new PermitSwarm());
const finalReturn = new Permit();
delete finalReturn['endPermitReason']; //The operand of a 'delete' operator must be optional.ts(2790)
expect(returnValue).toEqual(finalReturn);
});
发布于 2022-04-01 15:10:48
new Permit()
返回的任何内容都有一个类似这样的类型:
{
// other properties
endPermitReason: unknown;
}
它不是可选的,因此不能删除。您可以通过在类型/接口声明中添加?
使其成为可选的。
就像这样:
{
// other properties
endPermitReason?: unknown;
}
如果没有定义这些类型,那么就必须将finalReturn
断言为Partial<ReturnType<new Permit()>>
(重复检查语法)。
https://stackoverflow.com/questions/71708967
复制相似问题