blob: 06ae6486b8bf4682da6894422358d9b8d8b84ad2 [file] [edit]
// Copyright (C) 2026 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Assertion utilities for runtime validation and TypeScript type narrowing.
//
// These functions provide fail-fast semantics: if an assertion fails, an
// exception is thrown immediately. This makes bugs easier to catch and debug
// by surfacing issues at the point of failure rather than propagating invalid
// state.
//
// In addition to runtime checks, these assertions help TypeScript narrow types.
// For example, after calling assertExists(x), TypeScript knows x is non-null.
export function assertExists<A>(
value: A | null | undefined,
optMsg?: string,
): A {
if (value === null || value === undefined) {
throw new Error(optMsg ?? 'Value is null or undefined');
}
return value;
}
// assertExists trips over NULLs, but in many contexts NULL is a valid SQL value
// we have to work with.
export function assertDefined<T>(value: T | undefined, optMsg?: string): T {
if (value === undefined) {
throw new Error(optMsg ?? 'Value is undefined');
}
return value;
}
// Asserts that the value is an instance of the given class. Returns the value
// with a narrowed type if the assertion passes, otherwise throws an error.
export function assertIsInstance<T>(
value: unknown,
clazz: abstract new (...args: never[]) => T,
optMsg?: string,
): T {
assertTrue(
value instanceof clazz,
optMsg ?? `Value is not an instance of ${clazz.name}`,
);
return value as T;
}
export function assertTrue(value: boolean, optMsg?: string) {
if (!value) {
throw new Error(optMsg ?? 'Failed assertion');
}
}
export function assertFalse(value: boolean, optMsg?: string) {
assertTrue(!value, optMsg);
}
// This function serves two purposes.
// 1) A runtime check - if we are ever called, we throw an exception.
// This is useful for checking that code we suspect should never be reached is
// actually never reached.
// 2) A compile time check where typescript asserts that the value passed can be
// cast to the "never" type.
// This is useful for ensuring we exhaustively check union types.
export function assertUnreachable(value: never, optMsg?: string): never {
throw new Error(
optMsg ?? `This code should not be reachable ${value as unknown}`,
);
}
/**
* In TS 5.7, the TypedArrays and other array view and wrapper types were made
* to be generic, in order to distinguish those backed by ArrayBuffers vs
* SharedArrayBuffers, defaulting to a union of these two if the type is lefe
* unspecified (ArrayBufferLike). This is a good thing in general as the two
* buffer types have differing interfaces, however the main problem with this is
* that a lot of libraries have not been updated to reflect which buffer type
* they actually use and return.
*
* @see https://github.com/microsoft/TypeScript/issues/60579
*/
export function assertIsArrayBufferView(
view: ArrayBufferView,
): asserts view is ArrayBufferView<ArrayBuffer> {
if (view.buffer instanceof SharedArrayBuffer) {
// Copy the underlying buffer into the array, trimming to the byte bounds of the view
throw new Error(
"Underlying view is a SAB. If this is a problem we could convert it but we're being defensive as this could be expensive",
);
}
}