Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/three-mice-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"neverthrow": patch
---

Made err() infer strings narrowly for easier error tagging.
6 changes: 5 additions & 1 deletion src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ export type Result<T, E> = Ok<T, E> | Err<T, E>

export const ok = <T, E = never>(value: T): Ok<T, E> => new Ok(value)

export const err = <T = never, E = unknown>(err: E): Err<T, E> => new Err(err)
export function err<T = never, E extends string = string>(err: E): Err<T, E>
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do this with an overloaded type if you'd prefer, but figured this was the easiest way to make this change.

export function err<T = never, E = unknown>(err: E): Err<T, E>
export function err<T = never, E = unknown>(err: E): Err<T, E> {
return new Err(err)
}

/**
* Evaluates the given generator to a Result returned or an Err yielded from it,
Expand Down
24 changes: 21 additions & 3 deletions tests/typecheck-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,11 @@ type CreateTuple<L, V = string> =
});

(function it(_ = 'combines only err results into one') {
type Expectation = Result<[ never, never ], number | string>;
type Expectation = Result<[ never, never ], number | 'abc'>;

const result = Result.combine([
err(1),
err('string'),
err('abc'),
]);

const assignableToCheck: Expectation = result;
Expand Down Expand Up @@ -928,7 +928,7 @@ type CreateTuple<L, V = string> =
});

(function it(_ = 'combines only err results into one') {
type Expectation = Result<[ never, never ], [number, string]>;
type Expectation = Result<[ never, never ], [number, 'string']>;

const result = Result.combineWithAllErrors([
err(1),
Expand Down Expand Up @@ -999,6 +999,24 @@ type CreateTuple<L, V = string> =
});
});
});

(function describe(_ = 'err') {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't sure where this test should go, so made its own block.

(function it(_ = 'infers the error type narrowly when it is a string') {
type Expectation = Result<never, 'error'>

const result = err('error')

const assignableToCheck: Expectation = result;
});

(function it(_ = 'infers the error type widely when it is not a string') {
type Expectation = Result<never, { abc: number }>

const result = err({ abc: 123 })

const assignableToCheck: Expectation = result;
});
})
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note - prettier can't run on this file because it's an outdated version and there's an 'import type'.

When I did try to run prettier on it, a LOT of changes were made, probably to do with the semicolons.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-shaka mentioned we may want to use biome instead of prettier, but perhaps if we just update prettier this issue goes away?

});


Expand Down