signal-desktop/ts/util/missingCaseError.ts

27 lines
947 B
TypeScript
Raw Normal View History

// Copyright 2018-2022 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import { reallyJsonStringify } from './reallyJsonStringify';
2020-11-12 18:09:39 +00:00
2018-04-12 21:19:13 +00:00
// `missingCaseError` is useful for compile-time checking that all `case`s in
// a `switch` statement have been handled, e.g.
//
// type AttachmentType = 'media' | 'documents';
//
// const type: AttachmentType = selectedTab;
// switch (type) {
// case 'media':
2018-04-15 01:11:40 +00:00
// return <MediaGridItem/>;
2018-04-12 21:19:13 +00:00
// case 'documents':
2018-04-15 01:11:40 +00:00
// return <DocumentListItem/>;
2018-04-12 21:19:13 +00:00
// default:
// return missingCaseError(type);
// }
//
// If we extended `AttachmentType` to `'media' | 'documents' | 'links'` the code
// above would trigger a compiler error stating that `'links'` has not been
// handled by our `switch` / `case` statement which is useful for code
// maintenance and system evolution.
export const missingCaseError = (x: never): TypeError =>
new TypeError(`Unhandled case: ${reallyJsonStringify(x)}`);