Is there a way to consolidate the functions getStringOrFail
and getNumberOrFail
into a single function that handles different argument types?
export const getOrFail = <T>(value: T | null, message: string): T => {
if (value !== null) return value;
throw new Error(message);
}
I attempted using generics but encountered the issue of being able to return null
. How can I refactor this code to achieve my goal?