Skip to content

Commit 8dbc806

Browse files
committed
safeTry should not require .safeUnwrap()
1 parent ac52282 commit 8dbc806

9 files changed

+6601
-13844
lines changed

package-lock.json

+2,690-10,194
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"scripts": {
1212
"local-ci": "npm run typecheck && npm run lint && npm run test && npm run format && npm run build",
1313
"test": "jest && npm run test-types",
14+
"jest": "jest",
1415
"test-types": "tsc --noEmit -p ./tests/tsconfig.tests.json",
1516
"lint": "eslint ./src --ext .ts",
1617
"format": "prettier --write 'src/**/*.ts?(x)' && npm run lint -- --fix",
@@ -36,21 +37,22 @@
3637
"@babel/preset-typescript": "7.24.7",
3738
"@changesets/changelog-github": "^0.5.0",
3839
"@changesets/cli": "^2.27.7",
39-
"@types/jest": "27.4.1",
40+
"@jest/test-utils": "^0.0.0",
41+
"@types/jest": "29.5.12",
4042
"@types/node": "^18.19.39",
4143
"@typescript-eslint/eslint-plugin": "4.28.1",
4244
"@typescript-eslint/parser": "4.28.1",
43-
"babel-jest": "27.5.1",
45+
"babel-jest": "29.7.0",
4446
"eslint": "7.30.0",
4547
"eslint-config-prettier": "7.1.0",
4648
"eslint-plugin-prettier": "3.4.0",
47-
"jest": "27.5.1",
49+
"jest": "29.7.0",
4850
"prettier": "2.2.1",
4951
"rollup": "^4.18.0",
5052
"rollup-plugin-dts": "^6.1.1",
5153
"rollup-plugin-typescript2": "^0.32.1",
5254
"testdouble": "3.20.2",
53-
"ts-jest": "27.1.5",
55+
"ts-jest": "29.2.5",
5456
"ts-toolbelt": "9.6.0",
5557
"typescript": "4.7.2"
5658
},

src/result-async.ts

+12
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,18 @@ export class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
205205
): PromiseLike<A | B> {
206206
return this._promise.then(successCallback, failureCallback)
207207
}
208+
209+
async *[Symbol.asyncIterator](): AsyncGenerator<Err<never, E>, T> {
210+
const result = await this._promise
211+
212+
if (result.isErr()) {
213+
// @ts-expect-error -- This is structurally equivalent and safe
214+
yield errAsync(result.error)
215+
}
216+
217+
// @ts-expect-error -- This is structurally equivalent and safe
218+
return result.value
219+
}
208220
}
209221

210222
export const okAsync = <T, E = never>(value: T): ResultAsync<T, E> =>

src/result.ts

+14
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,11 @@ export class Ok<T, E> implements IResult<T, E> {
381381
_unsafeUnwrapErr(config?: ErrorConfig): E {
382382
throw createNeverThrowError('Called `_unsafeUnwrapErr` on an Ok', this, config)
383383
}
384+
385+
// eslint-disable-next-line require-yield
386+
*[Symbol.iterator](): Generator<Err<never, E>, T> {
387+
return this.value
388+
}
384389
}
385390

386391
export class Err<T, E> implements IResult<T, E> {
@@ -467,6 +472,15 @@ export class Err<T, E> implements IResult<T, E> {
467472
_unsafeUnwrapErr(_?: ErrorConfig): E {
468473
return this.error
469474
}
475+
476+
*[Symbol.iterator](): Generator<Err<never, E>, T> {
477+
// eslint-disable-next-line @typescript-eslint/no-this-alias
478+
const self = this
479+
// @ts-expect-error -- This is structurally equivalent and safe
480+
yield self
481+
// @ts-expect-error -- This is structurally equivalent and safe
482+
return self
483+
}
470484
}
471485

472486
export const fromThrowable = Result.fromThrowable

0 commit comments

Comments
 (0)