Made err() infer strings narrowly for easier error tagging#563
Conversation
🦋 Changeset detectedLatest commit: eadf50c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| 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> |
There was a problem hiding this comment.
Can do this with an overloaded type if you'd prefer, but figured this was the easiest way to make this change.
| }); | ||
| }); | ||
|
|
||
| (function describe(_ = 'err') { |
There was a problem hiding this comment.
Wasn't sure where this test should go, so made its own block.
|
|
||
| const assignableToCheck: Expectation = result; | ||
| }); | ||
| }) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@m-shaka mentioned we may want to use biome instead of prettier, but perhaps if we just update prettier this issue goes away?
|
Thanks for the contribution @mattpocock :) |
|
Just found this in the blame while working on #584. Nice work @mattpocock. It might be useful for |
I would like
errto infer strings more narrowly. This would make it easier to create unions out of possible string errors.This would infer as
Ok<number> | Err<'Too low'> | Err<'Too high'>.Let me know if this is desirable.