diff --git a/.changeset/mean-eagles-develop.md b/.changeset/mean-eagles-develop.md deleted file mode 100644 index 6bf9afdc..00000000 --- a/.changeset/mean-eagles-develop.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"neverthrow": patch ---- - -fix: change type definitions to make inferring types of safeTry more strict diff --git a/CHANGELOG.md b/CHANGELOG.md index e51cf60c..cae573dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # neverthrow +## 7.0.1 + +### Patch Changes + +- [#527](https://github.com/supermacro/neverthrow/pull/527) [`2e1f198`](https://github.com/supermacro/neverthrow/commit/2e1f19899800ce5e1164412c6a693cf2f1c40b20) Thanks [@3846masa](https://github.com/3846masa)! - fix: change type definitions to make inferring types of safeTry more strict + +- [#497](https://github.com/supermacro/neverthrow/pull/497) [`e06203e`](https://github.com/supermacro/neverthrow/commit/e06203e90b2b64edaa42707cbca8383c9f4765e8) Thanks [@braxtonhall](https://github.com/braxtonhall)! - enhance type inferrence of `match` + ## 7.0.0 ### Major Changes diff --git a/README.md b/README.md index 2a0f3335..d139360d 100644 --- a/README.md +++ b/README.md @@ -445,10 +445,10 @@ Match callbacks do not necessitate to return a `Result`, however you can return ```typescript class Result { - match( + match( okCallback: (value: T) => A, - errorCallback: (error: E) => A - ): A => { ... } + errorCallback: (error: E) => B + ): A | B => { ... } } ``` @@ -1064,10 +1064,10 @@ The difference with `Result.match` is that it always returns a `Promise` because ```typescript class ResultAsync { - match( + match( okCallback: (value: T) => A, - errorCallback: (error: E) => A - ): Promise => { ... } + errorCallback: (error: E) => B + ): Promise => { ... } } ``` diff --git a/package.json b/package.json index 139b6f17..b0bd0b45 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "neverthrow", - "version": "7.0.0", + "version": "7.0.1", "description": "Stop throwing errors, and instead return Results!", "main": "dist/index.cjs.js", "module": "dist/index.es.js", diff --git a/src/result-async.ts b/src/result-async.ts index e0624276..8f926cb6 100644 --- a/src/result-async.ts +++ b/src/result-async.ts @@ -147,7 +147,7 @@ export class ResultAsync implements PromiseLike> { ) } - match(ok: (t: T) => A, _err: (e: E) => A): Promise { + match(ok: (t: T) => A, _err: (e: E) => B): Promise { return this._promise.then((res) => res.match(ok, _err)) } diff --git a/src/result.ts b/src/result.ts index cc10a379..d046f4e3 100644 --- a/src/result.ts +++ b/src/result.ts @@ -232,7 +232,7 @@ interface IResult { * @param ok * @param err */ - match(ok: (t: T) => A, err: (e: E) => A): A + match(ok: (t: T) => A, err: (e: E) => B): A | B /** * Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`. @@ -309,7 +309,7 @@ export class Ok implements IResult { } // eslint-disable-next-line @typescript-eslint/no-unused-vars - match(ok: (t: T) => A, _err: (e: E) => A): A { + match(ok: (t: T) => A, _err: (e: E) => B): A | B { return ok(this.value) } @@ -380,7 +380,7 @@ export class Err implements IResult { return v } - match(_ok: (t: T) => A, err: (e: E) => A): A { + match(_ok: (t: T) => A, err: (e: E) => B): A | B { return err(this.error) } diff --git a/tests/typecheck-tests.ts b/tests/typecheck-tests.ts index 31be6bf5..c637d5ca 100644 --- a/tests/typecheck-tests.ts +++ b/tests/typecheck-tests.ts @@ -229,6 +229,54 @@ type CreateTuple = }); }); + (function describe(_ = 'match') { + (function it(_ = 'the type of the arguments match the types of the result') { + type OKExpectation = number + type ErrExpectation = string + + ok(123) + .match( + (val: OKExpectation): void => void val, + (val: ErrExpectation): void => void val, + ); + err("123") + .match( + (val: OKExpectation): void => void val, + (val: ErrExpectation): void => void val, + ); + }); + + (function it(_ = 'infers the resulting value from match callbacks (same type)') { + type Expectation = boolean + + const okResult: Expectation = ok(123) + .match( + (val) => !!val, + (val) => !!val, + ); + const errResult: Expectation = err('123') + .match( + (val) => !!val, + (val) => !!val, + ); + }); + + (function it(_ = 'infers the resulting value from match callbacks (different type)') { + type Expectation = boolean | bigint + + const okResult: Expectation = ok('123') + .match( + (val) => !!val, + (val) => BigInt(val), + ); + const errResult: Expectation = err(123) + .match( + (val) => !!val, + (val) => BigInt(val), + ); + }); + }); + (function describe(_ = 'asyncAndThen') { (function it(_ = 'Combines two equal error types (native scalar types)') { type Expectation = ResultAsync