I have the following:
exampleFunction = (param: string) => {
try {
const result = processString(param) as { resultExample: string };
return { isSuccess: true, ...result };
catch (error) {
return { isSuccess: false };
}
}
Ideally, the return would type as: { isSuccess: true, resultExample: string} | { isSuccess: false }, but it types as { isSuccess: boolean, resultExample: string } | { isSuccess: boolean }, which is frustrating because this means I can't leverage type guards.
const data = exampleFunction("exampleParam");
if (data.isSuccess) {
functionExpectingResultExample(data.resultExample); // Type Error because "resultExample: string | {}" is not assignable to "resultExample: string"
}
The above should not error because the true and false should be leveraged as type guards, but instead they're typed as booleans.
Is my only solution to manually type them as true and false?