gvergnaud / hotscript

A library of composable functions for the type-level! Transform your TypeScript types in any way you want using functions you already know.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature Request] Better error handling

gvergnaud opened this issue · comments

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.

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?