Creating a Generic `TaggedError` Factory Function in TypeScript

👋 hello all! minor devex question - I tried digging into the docs but could not find an answer, is there a way to make a generic TaggedError factory function? like so? as i understand it this causes issues with TS and potentially with the underlying effect Tagging system

export function createTaggedError<
  Tag extends `somethingSpecial/${string}`,
  Fields extends Record<string, any> = {},
>(tag: Tag) {
  return class GenericTaggedError extends Data.TaggedError(tag)<
    { message: string; cause?: unknown } & Fields
> {};
}


im familiar with generic/dynamic services syntax
const ServiceTag = Context.GenericTag<SomeDynamicService>(
    `somethingSpecial/${someArg}`,
  );

why? Im trying to reduce the amount of error boilerplate when creating tagged errors and their types, as i might want to force a certain object style be included in all errors, I also want to use TS to force certain naming conventions.

or perhaps there is different convention for error inheritance?
Was this page helpful?