Issue
I have written the following function to help with error handling
const capture = <T>(
callback: () => T
): { result?: T; error?: Error } => {
try {
return { result: callback(), error: undefined };
} catch (err) {
return { result: undefined, error: err as Error };
}
};
Example usage:
const { result, error } = capture<number>(() => foo());
if (error) badPath();
console.log("hooray we got a result", result)
However the TypeScript compiler will complain:
Object is possibly 'undefined'. ts(2532)
const result: number | undefined
I understand why the compiler is complaining (this is expected behaviour for using optional params).
However I was wondering if there existed some TypeScript shenanigans that could support conditional return types.
i.e. is there a way we could specify capture
's signature such that when error
doesn't exist, result
is inferred to exist? And vice versa?
Solution
That's not possible because TypeScript doesn't know about runtime.
The closest thing that comes to mind is to allow T
to be undefined
(callback: () => T | undefined)
and then filter it along with error
, making the rest of your code understand that result
at that point is a number
and can't be undefined.
const capture = <T>(callback: () => T | undefined): { result?: T; error?: Error } => {
try {
return { result: callback(), error: undefined }
} catch (err) {
return { result: undefined, error: err as Error }
}
}
const start = () => {
const { result, error } = capture<number>(() => foo())
if (error || !result) {
// something
return
}
// result // here it will be `number`
// (cannot be undefined because we checked)
console.log('hooray we got a result', result)
}
Answered By - BrunoLM
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.