From 66e6f0fbea88c384143116f66a050b932afa4a40 Mon Sep 17 00:00:00 2001 From: braxtonhall Date: Mon, 9 Oct 2023 15:08:59 +0200 Subject: [PATCH 1/6] Create tests --- tests/typecheck-tests.ts | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/typecheck-tests.ts b/tests/typecheck-tests.ts index 849b2247..7addb52a 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 val, + ); + err("123") + .match( + (val: OKExpectation): void => void val, + (val: ErrExpectation) => 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 = string | number + + const okResult: Expectation = ok(123) + .match( + (val) => String(val), + (val) => !!val, + ); + const errResult: Expectation = err(123) + .match( + (val) => String(val), + (val) => !!val, + ); + }); + }); + (function describe(_ = 'asyncAndThen') { (function it(_ = 'Combines two equal error types (native scalar types)') { type Expectation = ResultAsync From ae97b93f28cd63075f22263af014b8d414ffd171 Mon Sep 17 00:00:00 2001 From: braxtonhall Date: Mon, 9 Oct 2023 15:09:54 +0200 Subject: [PATCH 2/6] Implementation --- README.md | 12 ++++++------ src/result-async.ts | 2 +- src/result.ts | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0701f635..6b6cb2ae 100644 --- a/README.md +++ b/README.md @@ -441,10 +441,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 => { ... } } ``` @@ -1011,10 +1011,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/src/result-async.ts b/src/result-async.ts index 87285455..6416aabe 100644 --- a/src/result-async.ts +++ b/src/result-async.ts @@ -129,7 +129,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 c8f09971..492f164f 100644 --- a/src/result.ts +++ b/src/result.ts @@ -168,7 +168,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 /** * **This method is unsafe, and should only be used in a test environments** @@ -240,7 +240,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) } @@ -303,7 +303,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) } From ee824466b5ca1a1bf108c9f78b7b060d731014e1 Mon Sep 17 00:00:00 2001 From: braxtonhall Date: Mon, 9 Oct 2023 15:33:45 +0200 Subject: [PATCH 3/6] Formatting --- tests/typecheck-tests.ts | 44 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/tests/typecheck-tests.ts b/tests/typecheck-tests.ts index 7addb52a..f8539e73 100644 --- a/tests/typecheck-tests.ts +++ b/tests/typecheck-tests.ts @@ -236,44 +236,44 @@ type CreateTuple = ok(123) .match( - (val: OKExpectation): void => void val, - (val: ErrExpectation) => void val, + (val: OKExpectation): void => void val, + (val: ErrExpectation): void => void val, ); err("123") - .match( - (val: OKExpectation): void => void val, - (val: ErrExpectation) => void val, - ); + .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, - ); + .match( + (val) => !!val, + (val) => !!val, + ); const errResult: Expectation = err('123') - .match( - (val) => !!val, - (val) => !!val, - ); + .match( + (val) => !!val, + (val) => !!val, + ); }); (function it(_ = 'infers the resulting value from match callbacks (different type)') { type Expectation = string | number const okResult: Expectation = ok(123) - .match( - (val) => String(val), - (val) => !!val, - ); + .match( + (val) => String(val), + (val) => !!val, + ); const errResult: Expectation = err(123) - .match( - (val) => String(val), - (val) => !!val, - ); + .match( + (val) => String(val), + (val) => !!val, + ); }); }); From cc921aadb7164f5705fb4b3a7a5cf27949da0d3e Mon Sep 17 00:00:00 2001 From: braxtonhall Date: Mon, 9 Oct 2023 15:37:07 +0200 Subject: [PATCH 4/6] Fix a test --- tests/typecheck-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/typecheck-tests.ts b/tests/typecheck-tests.ts index f8539e73..c8f8ed85 100644 --- a/tests/typecheck-tests.ts +++ b/tests/typecheck-tests.ts @@ -262,7 +262,7 @@ type CreateTuple = }); (function it(_ = 'infers the resulting value from match callbacks (different type)') { - type Expectation = string | number + type Expectation = string | boolean const okResult: Expectation = ok(123) .match( From 304290c012e21a857ea121ac4f2e35a5b89f039d Mon Sep 17 00:00:00 2001 From: braxtonhall Date: Mon, 9 Oct 2023 15:38:51 +0200 Subject: [PATCH 5/6] Make a better test --- tests/typecheck-tests.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/typecheck-tests.ts b/tests/typecheck-tests.ts index c8f8ed85..84b768e2 100644 --- a/tests/typecheck-tests.ts +++ b/tests/typecheck-tests.ts @@ -262,17 +262,17 @@ type CreateTuple = }); (function it(_ = 'infers the resulting value from match callbacks (different type)') { - type Expectation = string | boolean + type Expectation = boolean | bigint - const okResult: Expectation = ok(123) + const okResult: Expectation = ok('123') .match( - (val) => String(val), (val) => !!val, + (val) => BigInt(val), ); - const errResult: Expectation = err(123) + const errResult: Expectation = err(123) .match( - (val) => String(val), (val) => !!val, + (val) => BigInt(val), ); }); }); From e06203e90b2b64edaa42707cbca8383c9f4765e8 Mon Sep 17 00:00:00 2001 From: m-shaka Date: Tue, 6 Aug 2024 09:19:29 +0900 Subject: [PATCH 6/6] Create tall-berries-guess.md --- .changeset/tall-berries-guess.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tall-berries-guess.md diff --git a/.changeset/tall-berries-guess.md b/.changeset/tall-berries-guess.md new file mode 100644 index 00000000..e564d676 --- /dev/null +++ b/.changeset/tall-berries-guess.md @@ -0,0 +1,5 @@ +--- +"neverthrow": patch +--- + +enhance type inferrence of `match`