Files
twenty/packages/twenty-server/src/utils/assert.ts
T
Félix Malfait 8b4b9ef8da Change type import rule (#13751)
Forcing "type" to be explicit, works best will rollup on the frontend to
exclude depdendencies
2025-08-08 01:27:05 +02:00

32 lines
742 B
TypeScript

import { type HttpException } from '@nestjs/common';
type Assert = (
condition: unknown,
message?: string,
ErrorType?: new (message?: string) => HttpException,
) => asserts condition;
/**
* assert condition and throws a HttpException
*/
export const assert: Assert = (condition, message, ErrorType) => {
if (!condition) {
if (ErrorType) {
if (message) {
throw new ErrorType(message);
}
throw new ErrorType();
}
throw new Error(message);
}
};
export const assertNotNull = <T>(item: T): item is NonNullable<T> =>
item !== null && item !== undefined;
export const assertNever = (_value: never, message?: string): never => {
throw new Error(message ?? "Didn't expect to get here.");
};