Within my codebase, there exists an assertion function that verifies a given value is an object (using the typeof
operator), with the exception of null
:
export function assertIsJavaScriptObjectExceptNull(value: unknown) {
if (typeof value !== 'object' || value === null) {
throw Error('Value is not an object.');
}
}
To enhance this function's utility in type narrowing scenarios, I am interested in incorporating an assertion signature. What would be the most appropriate type to specify for this purpose?
export function assertIsJavaScriptObjectExceptNull(value: unknown): asserts value is <type> {
if (typeof value !== 'object' || value === null) {
throw Error('Value is not an object.');
}
}
Should it be
{[key: string | number | symbol]: unknown}
, or Record<keyof unknown, unknown>
, or perhaps Object
, or another option entirely?
I aim to align with TypeScript's understanding of the correct type definition. It is worth noting that various TypeScript settings and potential JavaScript engine quirks could impact the outcome, leading to a lack of a definitive answer.