25 lines
709 B
TypeScript
25 lines
709 B
TypeScript
import type { Nullable } from './types.js';
|
|
import { NotNullExpected } from "./error.js";
|
|
|
|
export function must<T>(result: Nullable<T>): T {
|
|
if (result === null || result === undefined) {
|
|
throw new NotNullExpected();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function must_any<T>(result: Nullable<unknown>): T {
|
|
if (result === null || result === undefined) {
|
|
throw new NotNullExpected();
|
|
}
|
|
return result as T;
|
|
}
|
|
|
|
type ErrType<T> = { new(msg?: string): T }
|
|
|
|
export function must_err<T, ErrT extends Error>(result: Nullable<T>, errType: ErrType<ErrT>, errMsg?: string): T {
|
|
if (result === null || result === undefined) {
|
|
throw new errType(errMsg);
|
|
}
|
|
return result;
|
|
} |