badrap / result Goto Github PK
View Code? Open in Web Editor NEWA TypeScript result type taking cues from Rust's Result and Haskell's Either types
License: MIT License
A TypeScript result type taking cues from Rust's Result and Haskell's Either types
License: MIT License
Hello,
sometimes I jsut want to return an error but with no value, for the moment I do this :
return Result.err(new Error())
But I would prefer something like this
return Result.err()
Let's say I have the following result:
const result: Result<number, ErrorA> = someOperation();
and I want to chain like this:
// Assuming ErrorA and ErrorB are structurally different.
result.chain(value => {
if(value < 0) return err(new ErrorB());
else return ok(value);
});
then I get the error:
TS2345: Argument of type (value: number) => Result<number, ErrorA> | Result<never, ErrorB> is
not assignable to parameter of type (value: number) => Result<number, ErrorA>
Type Result<number, ErrorA> | Result<never, ErrorB> is not assignable to type
Result<number, ErrorA>
Type Ok<never, ErrorB> is not assignable to type Result<number, ErrorA>
Type Ok<never, ErrorB> is not assignable to type Ok<number, ErrorA>
Property x is missing in type ErrorB but required in type ErrorA
It seems like it is not possible to switch error type inside a chain with only a ok-mapper. The only workaround is:
result.chain<number, ErrorA | ErrorB>(
(value) => {
if (value < 0) return err(new ErrorB());
else return ok(value);
},
(error) => err(error)
);
but that is clearly too verbose.
Hi,
I am not sure how you generate index.d.ts
but it seems there is an issue with it as I get the following error when running tsc
:
$ tsc
node_modules/@badrap/result/dist/index.d.ts:1:1 - error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file.
1 abstract class _Result<T, E extends Error> {
~~~~~~~~
Found 1 error.
error Command failed with exit code 1.
I'm trying to chain an async function, and am getting a type error. Can anyone point me in the right direction?
const fooRes: Result<string, Error> = await foo();
const barRes = fooRes.chain(async e => await bar(e)).unwrap();
Argument of type 'e => Promise<Result<string, Error>>' is not assignable to parameter of type '(value: string) => Result<unknown, Error>'.
I'm liking the lib so far! What are your thoughts on orElse
methods? Right now if I want to have errors fallback I do:
const res = fn().chain(
(x) => Result.ok(x),
() => otherFn()
);
But that could be made a little more ergonomic if it was something like:
const res = fn().orElse((err) => otherFn());
Or the equivalent unwrap:
const val = fn().unwrapOrElse((err) => otherFn());
I get that it's a balance of keeping the lib small and having things be ergonomic - rust has soo many Result methods 😆
Thanks for the great library.
I would like to use the following
const res = Result.ok('test');
if (res.isOk) {
console.log(res.value);
} else {
console.log(res.error); // compile error
}
However, I get a compile error ”console.log(res.error);”, and I'm thinking the typescript compiler is not able to infer types.
It returns the following error
[tsserver 2339] [E] Property 'error' does not exist on type 'Result<string,Error>'. Property 'error' does not exist on type 'Ok<string, Error>'.
To pass through the compiler, Explicit type definitions are necessary, but not elegant.
const res = Result.ok('test');
if (res.isOk) {
console.log(res.value);
} else {
console.log((res as Result.Err<string, Error>).error);
}
Is there a simpler way to define it, or a way to customize the library itself?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.