Type Erasure for Tagged Errors in Helper Functions
I want to implement a utility function that erases a set of tagged errors. This is a toy example I've wrestled with for awhile but I can't figure out how to accomplish this. Any tips?
import { Data, Effect } from "effect"
class CustomError1 extends Data.TaggedError("CustomError1")<{
readonly message: string
}> {}
class CustomError2 extends Data.TaggedError("CustomError2")<{
readonly message: string
}> {}
const err1 = new CustomError1({ message: "This is a custom error 1" })
const err2 = new CustomError2({ message: "This is a custom error 2" })
// Effect.Effect<void, CustomError1 | CustomError2, never>
const program = Effect.gen(function*() {
yield* Effect.fail(err1)
yield* Effect.fail(err2)
return "success"
})
// Currently not a valid implementation. This is what I need to fix
const eraseCustomError1 = <E extends CustomError1, R>(e: Effect.Effect<string, E, R>) =>
e.pipe(
// IMPORTANT: would prefer to use catchTags or similar API here
// This is just a toy example but my real use for this requires having // This helper erase many errors
Effect.catchTags({
// IMPORTANT: I want to have this error parameter be strongly typed
// Somehow need to provide this via generic function args
CustomError1: (error: CustomError1) => Effect.succeed(error.message)
})
)
// Should have the following type
// Effect.Effect<void, CustomError1, never>
const onlyThrowsCustomError2 = program.pipe(eraseCustomError1)import { Data, Effect } from "effect"
class CustomError1 extends Data.TaggedError("CustomError1")<{
readonly message: string
}> {}
class CustomError2 extends Data.TaggedError("CustomError2")<{
readonly message: string
}> {}
const err1 = new CustomError1({ message: "This is a custom error 1" })
const err2 = new CustomError2({ message: "This is a custom error 2" })
// Effect.Effect<void, CustomError1 | CustomError2, never>
const program = Effect.gen(function*() {
yield* Effect.fail(err1)
yield* Effect.fail(err2)
return "success"
})
// Currently not a valid implementation. This is what I need to fix
const eraseCustomError1 = <E extends CustomError1, R>(e: Effect.Effect<string, E, R>) =>
e.pipe(
// IMPORTANT: would prefer to use catchTags or similar API here
// This is just a toy example but my real use for this requires having // This helper erase many errors
Effect.catchTags({
// IMPORTANT: I want to have this error parameter be strongly typed
// Somehow need to provide this via generic function args
CustomError1: (error: CustomError1) => Effect.succeed(error.message)
})
)
// Should have the following type
// Effect.Effect<void, CustomError1, never>
const onlyThrowsCustomError2 = program.pipe(eraseCustomError1)