我有这种类型:
export type NameValidationResult =
| {
success: true;
}
| {
success: false;
errorMessage: string;
};
我在这部分代码中使用了这种类型:
let nameValidationResult: NameValidationResult = this.getNameValidation();
if (nameValidationResult.success) {
return "this is success";
}
return nameValidationResult.errorMessage;
}
当我编译时,没有错误(也没有运行时错误)
我有这个测试:
describe("name validator tests", () => {
test("test name validator success", async () => {
const result: string = await NameValidator.validateName("testname");
if (result != "this is success") {
fail("fail test");
}
});
});
当我运行 Jest 时,我收到此错误:
Property 'errorMessage' does not exist on type '{ success: true; }
return nameValidationResult.errorMessage;
当我运行此代码时:
我该如何解决这个问题?
问题是测试没有运行,我设置了一个断点但我没有达到它。 如果我删除该行:
return nameValidationResult.errorMessage;
用线
return "Error message"
然后测试运行并通过(也在断点处停止)