Git Product home page Git Product logo

Comments (11)

Visual-Dawg avatar Visual-Dawg commented on June 11, 2024

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.

JUSTIVE avatar JUSTIVE commented on June 11, 2024

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.

JUSTIVE avatar JUSTIVE commented on June 11, 2024

I'll post a type that meets your request later here, let me know what you think!

from ts-belt.

Visual-Dawg avatar Visual-Dawg commented on June 11, 2024

@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.

JUSTIVE avatar JUSTIVE commented on June 11, 2024

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.

Visual-Dawg avatar Visual-Dawg commented on June 11, 2024

Sounds cool, thank you :)

from ts-belt.

JUSTIVE avatar JUSTIVE commented on June 11, 2024

just finished making a simple proof-of-concept.
here's link

from ts-belt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.