Skip to content

Commit

Permalink
use null instead of undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jan 20, 2025
1 parent 43bb6a2 commit 847a4ec
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions polyfill.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -25,12 +25,12 @@ type ErrorResult = ErrorObjectResult & ErrorTupleResult
/**
* Value result type expressed as object
*/
type ValueObjectResult<V> = { ok: true; error: null; value: V }
type ValueObjectResult<V> = { ok: true; error: undefined; value: V }

/**
* Value result type expressed as tuple
*/
type ValueTupleResult<V> = [ok: true, error: null, value: V]
type ValueTupleResult<V> = [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}
Expand All @@ -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 <V>(...args: ValueTupleResult<V> | ErrorTupleResult): Result<V>
Expand Down
8 changes: 2 additions & 6 deletions polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

/** @type {ResultConstructor} */
class Result {
ok = false
error = null
value = null

constructor(ok, error, value) {
this.ok = ok
this.error = error
Expand All @@ -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)
}
}

0 comments on commit 847a4ec

Please sign in to comment.