Skip to content

Commit

Permalink
Add internal utility function assertsUnreachable()
Browse files Browse the repository at this point in the history
  • Loading branch information
shrinktofit committed Sep 14, 2023
1 parent f4aa7e9 commit 6934ddb
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cocos/core/data/utils/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,32 @@ export function assertIsTrue (expr: unknown, message?: string): asserts expr {
export function assertsArrayIndex<T> (array: T[], index: number): void {
assertIsTrue(index >= 0 && index < array.length, `Array index ${index} out of bounds: [0, ${array.length})`);
}

/**
* Asserts the caller code's reachability.
*
* @example
* ```ts
* enum Color { RED, GREEN, BLUE }
*
* function toHex(colorThatDefinitelyCannotBeRed: Color): string {
* switch(colorThatDefinitelyCannotBeRed) {
* case Color.GREEN: return '0x00FF00';
* case Color.BLUE: return '0x0000FF';
*
* // Without this:
* // - tsc reports error ts(2366).
* // - eslint reports error about 'consistent-return' and 'default-case'.
* default: return assertsUnreachable();
* }
* }
* ```
*
* @note This function throws in debug mode and returns `undefined` otherwise.
*/
export function assertsUnreachable (): never {
if (DEBUG) {
throw new Error('Here should never be reachable');
}
return undefined as never;
}

0 comments on commit 6934ddb

Please sign in to comment.