Refactoring: Split shared types into seperate modules and document alot.
This commit is contained in:
parent
e08ae944c4
commit
843cd37243
31 changed files with 2498 additions and 842 deletions
server/shared/utils
30
server/shared/utils/enums.ts
Normal file
30
server/shared/utils/enums.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
* Utility functions for enums.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Helper function to detect unhandled enum fields in `switch` statements at compile time. In case this function
|
||||
* is called at runtime anyway (which should not happen) it throws a runtime error.
|
||||
*
|
||||
* In the example below the compiler will complain if not for all fields of `Enum` a corresponding `case` statement
|
||||
* exists.
|
||||
*
|
||||
* @param field - Unhandled field, the value being switched over.
|
||||
* @throws {@link Error} - If the function is called at runtime.
|
||||
*
|
||||
* @example
|
||||
* switch (enumValue) {
|
||||
* case Enum.FIELD1:
|
||||
* return;
|
||||
* case Enum.FIELD2:
|
||||
* return;
|
||||
*
|
||||
* ...
|
||||
*
|
||||
* default:
|
||||
* return unhandledEnumField(enumValue);
|
||||
* }
|
||||
*/
|
||||
export function unhandledEnumField(field: never): never {
|
||||
throw new Error(`Unhandled enum field: ${field}`);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue