index.js 990 B

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * Helper function for exhaustive checks of discriminated unions.
  5. * https://basarat.gitbooks.io/typescript/docs/types/discriminated-unions.html
  6. *
  7. * @example
  8. *
  9. * type A = {type: 'a'};
  10. * type B = {type: 'b'};
  11. * type Union = A | B;
  12. *
  13. * function doSomething(arg: Union) {
  14. * if (arg.type === 'a') {
  15. * return something;
  16. * }
  17. *
  18. * if (arg.type === 'b') {
  19. * return somethingElse;
  20. * }
  21. *
  22. * // TS will error if there are other types in the union
  23. * // Will throw an Error when called at runtime.
  24. * // Use `assertNever(arg, true)` instead to fail silently.
  25. * return assertNever(arg);
  26. * }
  27. */
  28. function assertNever(value, noThrow) {
  29. if (noThrow) {
  30. return value;
  31. }
  32. throw new Error("Unhandled discriminated union member: " + JSON.stringify(value));
  33. }
  34. exports.assertNever = assertNever;
  35. exports.default = assertNever;