diff --git a/README.md b/README.md index 0b7eb54..f54e828 100644 --- a/README.md +++ b/README.md @@ -253,8 +253,8 @@ The `Result` class represents the form of the value returned by the `try` operat A `Result` instance contains three properties: - **`ok`**: A boolean indicating whether the expression executed successfully. - - **`error`**: The error thrown during execution, or `null` if no error occurred. - - **`value`**: The data returned from the execution, or `null` if an error occurred. + - **`error`**: The error thrown during execution, or `undefined` if no error occurred. + - **`value`**: The data returned from the execution, or `undefined` if an error occurred. Example usage: @@ -280,7 +280,7 @@ The `Result` class represents the form of the value returned by the `try` operat ```js // Creating a successful result - const result = new Result(true, null, value) + const result = new Result(true, undefined, value) const result = Result.ok(value) // Creating an error result diff --git a/polyfill.d.ts b/polyfill.d.ts index 7ca4aa1..c325b73 100644 --- a/polyfill.d.ts +++ b/polyfill.d.ts @@ -8,14 +8,14 @@ /** * Error result type expressed as object */ -type ErrorObjectResult = { ok: false; error: unknown; value: null } +type ErrorObjectResult = { ok: false; error: unknown; value: undefined } /** * Error result type expressed as tuple. * * - `error` type depends on `useUnknownInCatchVariables` tsconfig option */ -type ErrorTupleResult = [ok: false, error: unknown, value: null] +type ErrorTupleResult = [ok: false, error: unknown, value: undefined] /** * An error result is a object that can be either destructured {@link ErrorObjectResult} or accessed by index {@link ErrorTupleResult} @@ -25,12 +25,12 @@ type ErrorResult = ErrorObjectResult & ErrorTupleResult /** * Value result type expressed as object */ -type ValueObjectResult = { ok: true; error: null; value: V } +type ValueObjectResult = { ok: true; error: undefined; value: V } /** * Value result type expressed as tuple */ -type ValueTupleResult = [ok: true, error: null, value: V] +type ValueTupleResult = [ok: true, error: undefined, value: V] /** * A value result is a object that can be either destructured {@link ValueObjectResult} or accessed by index {@link ValueTupleResult} @@ -48,7 +48,7 @@ interface ResultConstructor { * * @example * - * new Result(true, null, 42) + * new Result(true, undefined, 42) * new Result(false, new Error('Something went wrong')) */ new (...args: ValueTupleResult | ErrorTupleResult): Result diff --git a/polyfill.js b/polyfill.js index f5f4b73..94fd3d4 100644 --- a/polyfill.js +++ b/polyfill.js @@ -2,10 +2,6 @@ /** @type {ResultConstructor} */ class Result { - ok = false - error = null - value = null - constructor(ok, error, value) { this.ok = ok this.error = error @@ -19,10 +15,10 @@ class Result { } static ok(value) { - return new Result(true, null, value) + return new Result(true, undefined, value) } static error(error) { - return new Result(false, error, null) + return new Result(false, error, undefined) } }