@sinchsmb/ui-kit
Version:
UI kit for SinchSMB frontend
47 lines (45 loc) • 1.14 kB
text/typescript
/* istanbul ignore file */
/**
* A utility for checking that all values from enum have been processed into the switch case.
*
* For example you have enum:
* ```
* enum TestEnum {
* One,
* Two,
* Three
* }
* ```
*
* And want process all values from enum. You write something like this:
* ```
* const value: TestEnum = ...;
*
* switch (value) {
* case TestEnum.One: ...; break;
* case TestEnum.Two: ...; break;
* case TestEnum.Three: ...; break;
* }
* ```
*
* But you want protect this code from case when you add new value to enum and forgot process it in
* switch-case. For this you can write:
* ```
* const value: TestEnum = ...;
*
* switch (value) {
* case TestEnum.One: ...; break;
* case TestEnum.Two: ...; break;
* case TestEnum.Three: ...; break;
* default: assertUnreachable(value);
* }
* ```
*
* And if you add `TestEnum.Four`, then you get TS error in `default`.
*
* @see https://stackoverflow.com/a/39419171/1778685
*/
export function assertUnreachable(value: never): never;
export function assertUnreachable<T>(value: T) {
throw new Error(`Didn't expect: ${value}`);
}