Here's a strange question I've been grappling with lately.
So, I've got this function that essentially creates a custom Alert Window to handle unsaved form data when a user tries to navigate away. It may or may not be triggered depending on the situation.
export const unsavedDataGuard: CanDeactivateFn<boolean> = async () => {
const utService: UTService = inject(utService);
const alertService: AlertService = inject(AlertService);
const unsaved = utService.unsaved;
if (unsaved) {
await alertService
.openQuestion('Cancel?')
.afterClosed()
.subscribe(result => {
if (result) {
return true;
} else {
return false;
}
});
} else {
return true;
}
};
The issue at hand is the error message I'm receiving:
Type '() => Promise<true | undefined>' is not assignable to type 'CanDeactivateFn<boolean>'.
Along with this warning:
Not all code paths return a value.
I get what the error is pointing to and where it stems from. If I simply return true or false after the await call, the error disappears. However, this deems the entire function ineffective as the alert will still pop up but won't serve any purpose since its buttons will become non-functional.
What perplexes me is why using the standard javascript confirmation window doesn't trigger an error:
confirm("Press a button!");
Since it should theoretically behave in a similar manner.