Add missingCaseError
function
This commit is contained in:
parent
fc1c3aabf5
commit
923d5ff088
1 changed files with 21 additions and 0 deletions
21
ts/missingCaseError.ts
Normal file
21
ts/missingCaseError.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
// `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':
|
||||
// return <ImageThumbnail/>;
|
||||
// case 'documents':
|
||||
// return <DocumentListEntry/>;
|
||||
// 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: ${x}`);
|
Loading…
Add table
Add a link
Reference in a new issue