assert.ts 488 B

12345678910111213141516171819202122232425
  1. // this function can be used to ensure there are no missing cases in the handling of a union type
  2. export function assertNever(x: never): never {
  3. throw new Error(`Unexpected object: ${x}`);
  4. }
  5. export function assert(expression: boolean, msg: string) {
  6. if (!expression) {
  7. throw new Error(msg);
  8. }
  9. }
  10. export function assertThrows(callback, msg) {
  11. let threw = false;
  12. try {
  13. callback()
  14. }
  15. catch (e) {
  16. threw = true;
  17. }
  18. if (!threw) {
  19. throw new Error(msg);
  20. }
  21. }