'TypeScript Generics: Create a Generic Error Handler Function
I am having an error handler which gets two parameters, error which is of type unknown. It can be an object, a string, etc. and then return value which is of generic type. It can be an empty string, empty array or an empty object.
Here's how my function looks:
const genericErrorHandler = <T>(error: any, returnValue: T): T => {
console.log('Error = ', error);
return returnValue;
};
But I am not sure about calling this function. Here's how I expect it to behave:
genericErrorHandler({statusCode:404},"Not Found") should return "Not Found"
genericErrorHandler("Internal Server Error", {}) should return {}
genericErrorHandler({message:"Invalid TOken"},[]) should return []
with their respective logging. How do I call this function for above 3 cases and do I need to modify my actual function too?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
