result.d.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * A Result wraps up a success state and a failure state, allowing you to
  3. * return a single type from a function and discriminate between the two
  4. * possible states in a principled way.
  5. *
  6. * Using it could look something like this:
  7. *
  8. * ```ts
  9. * import { result } from '@utils';
  10. *
  11. * const mightFail = (input: number): Result<number, string> => {
  12. * try {
  13. * let value: number = calculateSomethingWithInput(input);
  14. * return result.ok(value);
  15. * } catch (e) {
  16. * return result.err(e.message);
  17. * }
  18. * }
  19. *
  20. * const sumResult = mightFail(2);
  21. *
  22. * const msg = result.map(sumResult, (sum: number) => `the sum was: ${sum}`);
  23. * ```
  24. *
  25. * A few utility methods are defined in this module, like `map` and `unwrap`,
  26. * which are (probably obviously) inspired by the correspond methods on
  27. * `std::result::Result` in Rust.
  28. */
  29. export type Result<OnSuccess, OnFailure> = Ok<OnSuccess> | Err<OnFailure>;
  30. /**
  31. * Type for the Ok state of a Result
  32. */
  33. type Ok<T> = {
  34. isOk: true;
  35. isErr: false;
  36. value: T;
  37. };
  38. /**
  39. * Type for the Err state of a Result
  40. */
  41. type Err<T> = {
  42. isOk: false;
  43. isErr: true;
  44. value: T;
  45. };
  46. /**
  47. * Create an `Ok` given a value. This doesn't do any checking that the value is
  48. * 'ok-ish' since doing so would make an undue assumption about what is 'ok'.
  49. * Instead, this trusts the user to determine, at the call site, whether
  50. * something is `ok()` or `err()`.
  51. *
  52. * @param value the value to wrap up in an `Ok`
  53. * @returns an Ok wrapping the value
  54. */
  55. export declare const ok: <T>(value: T) => Ok<T>;
  56. /**
  57. * Create an `Err` given a value.
  58. *
  59. * @param value the value to wrap up in an `Err`
  60. * @returns an Ok wrapping the value
  61. */
  62. export declare const err: <T>(value: T) => Err<T>;
  63. /**
  64. * Map across a `Result`.
  65. *
  66. * If it's `Ok`, unwraps the value, applies the supplied function, and returns
  67. * the result, wrapped in `Ok` again. This could involve changing the type of
  68. * the wrapped value, for instance:
  69. *
  70. * ```ts
  71. * import { result } from "@utils";
  72. *
  73. * const myResult: Result<string, string> = result.ok("monads???");
  74. * const updatedResult = result.map(myResult, wrappedString => (
  75. * wrappedString.split("").length
  76. * ));
  77. * ```
  78. *
  79. * after the `result.map` call the type of `updatedResult` will now be
  80. * `Result<number, string>`.
  81. *
  82. * If it's `Err`, just return the same value.
  83. *
  84. * This lets the programmer trigger an action, or transform a value, only if an
  85. * earlier operation succeeded, short-circuiting instead if an error occurred.
  86. *
  87. * @param result a `Result` value which we want to map across
  88. * @param fn a function for handling the `Ok` case for the `Result`
  89. * @returns a new `Result`, with the a new wrapped value (if `Ok`) or the
  90. * same (if `Err)
  91. */
  92. export declare function map<T1, T2, E>(result: Result<T1, E>, fn: (t: T1) => Promise<T2>): Promise<Result<T2, E>>;
  93. export declare function map<T1, T2, E>(result: Result<T1, E>, fn: (t: T1) => T2): Result<T2, E>;
  94. /**
  95. * Unwrap a {@link Result}, return the value inside if it is an `Ok` and
  96. * throw with the wrapped value if it is an `Err`.
  97. *
  98. * @throws with the wrapped value if it is an `Err`.
  99. * @param result a result to peer inside of
  100. * @returns the wrapped value, if `Ok`
  101. */
  102. export declare const unwrap: <T, E>(result: Result<T, E>) => T;
  103. /**
  104. * Unwrap a {@link Result}, return the value inside if it is an `Err` and
  105. * throw with the wrapped value if it is an `Ok`.
  106. *
  107. * @throws with the wrapped value if it is an `Ok`.
  108. * @param result a result to peer inside of
  109. * @returns the wrapped value, if `Err`
  110. */
  111. export declare const unwrapErr: <T, E>(result: Result<T, E>) => E;
  112. export {};