Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Better error handling #6

Open
gvergnaud opened this issue Feb 13, 2023 · 1 comment
Open

[Feature Request] Better error handling #6

gvergnaud opened this issue Feb 13, 2023 · 1 comment

Comments

@gvergnaud
Copy link
Owner

We should have an error path in all pipeable functions so that they can throw when their input is incorrect. Not sure what this would look like yet.

@ggrandi
Copy link
Contributor

ggrandi commented Feb 13, 2023

One possible way we could handle errors would be like this: (playground link)

declare const ErrorMessage: unique symbol;
type Error<E> = {
  [ErrorMessage]: E
}

// redefine `Pipe` to stop if the accumulator has an error (avoids creating a temporary variable)
type Pipe<acc, xs extends Fn[]> =
  typeof ErrorMessage extends keyof acc
  ? acc
  : xs extends [
    infer first extends Fn,
    ...infer rest extends Fn[]
  ]
  ? Pipe<Call<first, acc>, rest>
  : acc;

Which could be used like this:

interface ThrowIfNotStringifiable extends Fn {
  output: this["args"][0] extends S.Stringifiable ? this["args"][0] : Error<"Expected a string">
}

type SayMessage<T> = Pipe<T, [ThrowIfNotStringifiable, S.Prepend<"Message: ">]>

type R1 = SayMessage<symbol>
// expect `type R1 = { [ErrorMessage]: "Expected a String" }`

type R2 = SayMessage<1>
// expect `type R2 = "Message: 1"`

Something like this would ensure that the only things with the typeof ErrorMessage key are actually errors and would force pipe to stop.

What i am not sure about is what to do for all of the higher-order functions. For example in T.Map:

type R3 = Pipe<[symbol, 1], [T.Map<ThrowIfNotStringifiable>]>
//       ^? [Error<"Expected a string">, 1]

Should we change it so it fails as soon as it encounters one error or should it wait and process the whole tuple and fail silently?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants