Comments (11)
It looks like that there is no perfect solution - but changing the signature of R.isOk
to
declare function isOk<A, unknown >(result: Result<A, unknown>): result is Ok<A>;
Makes it a lot more compatible. Same goes for R.isError
from ts-belt.
I think the fn
type should define its result type. In your example, the fn
's type signature is function fn(a: number): R.Error<string> | R.Ok<string> | R.Error<{ value: number; message: string; }>
, in which the Error type is separated. that's not even possible with rescript code.
let fn = x => {
switch x {
| x if Float.isNaN(x) => Error("Number is NaN")
| x if x > 10. => Ok(x)
| _ => Error({value: x, message: "Number too small"})
// ^ where error occurs, because the type system inferences
// fn's return type as result<float, string> from the first match clause.
}
}
in rescript, we can fix by describing the custom error type, in this case, a variant represents two cases of the errors.
type myError = JustError(string) | ErrorWithMessage({value: float, message: string})
let fn = (x: float): result<float, myError> => {
switch x {
| x if Float.isNaN(x) => Error(JustError("Number is NaN"))
| x if x > 10. => Ok(x)
| _ => Error(ErrorWithMessage({value: x, message: "Number too small"}))
}
}
Maybe some black magic of type-level programming in typescript would handle this, but I think It'll hurt the signatures of functions in the Result module's readability(yes, this change should be applied to all the functions in result module, not just isOk and isError, since the input value, in this case the return type of the fn function is not sanitized).
So I think, in this case you can just write your own error type, not changing signatures of the functions.
type MyError = string | {
value:number,
message:string
}
function fn(a: number):R.Result<number,MyError> {
if (!G.isNumber(a)) return R.Error("Number is NaN")
return a > 10
? R.Ok(a)
: R.Error({ value: a, message: "Number too small" })
}
const x = R.isOk(fn(10)) // => no more errors
from ts-belt.
I'll post a type that meets your request later here, let me know what you think!
from ts-belt.
@JUSTIVE Hey, thank you for the detailed response! :)
For errors this could work, however, within a pipe Ok
types are often implicitly defined, and being able to pass to R.isOk
a union of R.Ok<string> | R.Ok<something lese> | R.Error
to filter out the errors, does seem like a good use case.
But, if this is a ReScript limitation, there also doesnt seem to be a good solution to this ,except manually changing the generated Typescript code, which is not optimal
from ts-belt.
rescript, which is the basis of ts-belt, does not work like this because it uses a stricter type system than typescript. Since typescript is much more flexible than rescript, there seems to be a way if you want to try it. I'm currently working on a trick using hotscript
, and I'd like to share it with you as soon as it's completed.
from ts-belt.
Sounds cool, thank you :)
from ts-belt.
just finished making a simple proof-of-concept.
here's link
from ts-belt.
Related Issues (20)
- Can you tell me how to handle multiple Option types? HOT 4
- A.dropWhile is completely broken HOT 1
- `A.take` does not return empty array when `n` greater than length of array (as documented) HOT 2
- foy for the building not working properly HOT 2
- Update benchmarks to latest package versions HOT 4
- Would a D.deleteOtherKeys be useful as part of the core? HOT 1
- F.getWithDefault Coercion type problem HOT 5
- Unsafe `isNone()` and `isSome()` type predicates HOT 1
- Ts-belt is 4x slower because it uses the arguments object
- Why an Option type instead of `type` | undefined? HOT 4
- ts-belt usage example flagged by TypeScript HOT 2
- why not `Do Notation` and `Applicative`
- Misleading annotations for Array.range
- Is this library still being maintained? HOT 1
- 🚧 Announcement: the current state of ts-belt 🚧 HOT 2
- `AR.isOk` and `AR.isError` not defined
- `AR.toOption` for `undefined` promise resolutions resulting in `{ BS_PRIVATE_NESTED_SOME_NONE: 0 }` HOT 1
- O.Option's functions with null literal value behaves differ from with undefined literal values HOT 1
- Bad Typing Ok / Error HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-belt.