ente/web/packages/utils/ensure.ts
2024-04-19 16:20:20 +05:30

17 lines
448 B
TypeScript

/**
* Throw an exception if the given value is undefined.
*/
export const ensure = <T>(v: T | undefined): T => {
if (v === undefined) throw new Error("Required value was not found");
return v;
};
/**
* Throw an exception if the given value is not a string.
*/
export const ensureString = (v: unknown): string => {
if (typeof v != "string")
throw new Error(`Expected a string, instead found ${String(v)}`);
return v;
};