// or import * as Result from "@alanscodelog/utils/Result.js" // to be able to do Result.Ok and Result.Err // but you'll have to use Result.Result or Result.Type for the type
Note that to make this properly work when a function returns multiple errors, you must pass the KnownError types as a dricriminated union for them to be distinguishable based on some property (e.g. code). For example: KnownError<TypeA> | KnownError<TypeB>, NOT KnownError<TypeA | TypeB>. You can create a helper type like this to discriminate the generic KnownError class:
Barebones result monad.
Example with custom error, also see TypedError.
Note that to make this properly work when a function returns multiple errors, you must pass the KnownError types as a dricriminated union for them to be distinguishable based on some property (e.g.
code
). For example:KnownError<TypeA> | KnownError<TypeB>
, NOTKnownError<TypeA | TypeB>
. You can create a helper type like this to discriminate the generic KnownError class: