TaggedError couples `_tag` and `Error.name` — can't namespace the discriminant without rewriting the display name
Summary
TaggedError(tag) uses tag as both the _tag discriminant and the Error.name. There's no way to set one without the other, so namespacing the discriminant for collision-safety also rewrites the human-facing error name in stack traces and logs.
Where
dist/index.mjs (the TaggedError base constructor) hardcodes:
this._tag = tag;
this.name = tag;
The type doc confirms the intent: "the discriminant value, also used as the error name."
Why it matters
_tag is a global discriminator used with matchTags. If a consumer feeds errors from multiple libraries into one matchTags, generic tags like "TechnicalError" or "RetryableError" can collide and mis-dispatch. The natural fix is to namespace the tag ("@my-lib/TechnicalError") — but doing so makes Error.name become @my-lib/TechnicalError, which:
- leaks the slash-prefixed name into stack traces and logs, and
- breaks any
error.name === "TechnicalError"checks.
The only workaround today is overriding this.name in every subclass constructor — untyped boilerplate that's easy to forget:
class RetryableError extends TaggedError("@my-lib/RetryableError")<{ message: string }> {
constructor(message: string) {
super({ message });
this.name = "RetryableError"; // manual, repeated per class
}
}
(Concretely: I had to do exactly this across six error classes in a downstream consumer, and it forced a design escalation because namespacing silently rewrote Error.name.)
Proposal
Let the display name be chosen independently of the discriminant. Some options:
// (a) optional second arg
TaggedError("@my-lib/RetryableError", { name: "RetryableError" })
// (b) object form
TaggedError({ tag: "@my-lib/RetryableError", name: "RetryableError" })
// (c) default `name` to the last path segment of a namespaced tag
// "@my-lib/RetryableError" -> name "RetryableError", _tag unchanged
Backward compatible: when no name is supplied, default to tag (current behavior).