cal.pub0.org/lib/asStringOrNull.tsx

24 lines
599 B
TypeScript

export function asStringOrNull(str: unknown) {
return typeof str === "string" ? str : null;
}
export function asStringOrUndefined(str: unknown) {
return typeof str === "string" ? str : undefined;
}
export function asNumberOrUndefined(str: unknown) {
return typeof str === "string" ? parseInt(str) : undefined;
}
export function asNumberOrThrow(str: unknown) {
return parseInt(asStringOrThrow(str));
}
export function asStringOrThrow(str: unknown): string {
const type = typeof str;
if (type !== "string") {
throw new Error(`Expected "string" - got ${type}`);
}
return str;
}