Skip to content

Commit

Permalink
Merge branch 'master' into schniz/remove-maintainer-callout
Browse files Browse the repository at this point in the history
  • Loading branch information
supermacro authored Aug 15, 2024
2 parents 8aac6ae + 599f28b commit 3eb76d1
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 16 deletions.
5 changes: 0 additions & 5 deletions .changeset/mean-eagles-develop.md

This file was deleted.

8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,10 @@ Match callbacks do not necessitate to return a `Result`, however you can return

```typescript
class Result<T, E> {
match<A>(
match<A, B = A>(
okCallback: (value: T) => A,
errorCallback: (error: E) => A
): A => { ... }
errorCallback: (error: E) => B
): A | B => { ... }
}
```

Expand Down Expand Up @@ -1064,10 +1064,10 @@ The difference with `Result.match` is that it always returns a `Promise` because

```typescript
class ResultAsync<T, E> {
match<A>(
match<A, B = A>(
okCallback: (value: T) => A,
errorCallback: (error: E) => A
): Promise<A> => { ... }
errorCallback: (error: E) => B
): Promise<A | B> => { ... }
}
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/result-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
)
}

match<A>(ok: (t: T) => A, _err: (e: E) => A): Promise<A> {
match<A, B = A>(ok: (t: T) => A, _err: (e: E) => B): Promise<A | B> {
return this._promise.then((res) => res.match(ok, _err))
}

Expand Down
6 changes: 3 additions & 3 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ interface IResult<T, E> {
* @param ok
* @param err
*/
match<A>(ok: (t: T) => A, err: (e: E) => A): A
match<A, B = A>(ok: (t: T) => A, err: (e: E) => B): A | B

/**
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
Expand Down Expand Up @@ -309,7 +309,7 @@ export class Ok<T, E> implements IResult<T, E> {
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
match<A>(ok: (t: T) => A, _err: (e: E) => A): A {
match<A, B = A>(ok: (t: T) => A, _err: (e: E) => B): A | B {
return ok(this.value)
}

Expand Down Expand Up @@ -380,7 +380,7 @@ export class Err<T, E> implements IResult<T, E> {
return v
}

match<A>(_ok: (t: T) => A, err: (e: E) => A): A {
match<A, B = A>(_ok: (t: T) => A, err: (e: E) => B): A | B {
return err(this.error)
}

Expand Down
48 changes: 48 additions & 0 deletions tests/typecheck-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,54 @@ type CreateTuple<L, V = string> =
});
});

(function describe(_ = 'match') {
(function it(_ = 'the type of the arguments match the types of the result') {
type OKExpectation = number
type ErrExpectation = string

ok<number, string>(123)
.match(
(val: OKExpectation): void => void val,
(val: ErrExpectation): void => void val,
);
err<number, string>("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<number, string>(123)
.match(
(val) => !!val,
(val) => !!val,
);
const errResult: Expectation = err<number, string>('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<string, number>('123')
.match(
(val) => !!val,
(val) => BigInt(val),
);
const errResult: Expectation = err<string, number>(123)
.match(
(val) => !!val,
(val) => BigInt(val),
);
});
});

(function describe(_ = 'asyncAndThen') {
(function it(_ = 'Combines two equal error types (native scalar types)') {
type Expectation = ResultAsync<unknown, string>
Expand Down

0 comments on commit 3eb76d1

Please sign in to comment.