Skip to content

errorSubclassProperties

Reports incorrect Error subclass definitions that don't follow best practices.

✅ This rule is included in the ts logicalStrict presets.

When creating custom error classes that extend Error, there are several conventions that should be followed to ensure the errors work correctly and are easy to debug.

This rule reports:

  • Invalid class names: should be capitalized and end with Error
  • Missing name property: required for proper error identification
  • Using this.constructor.name: won’t work after minification
  • Mismatched name values: should match the class name
  • Redundant this.message assignments: already set by super()
class foo extends Error {
constructor(message: string) {
super(message);
this.name = "foo";
}
}
class CustomError extends Error {
constructor(message: string) {
super(message);
}
}
class CustomError extends Error {
constructor(message: string) {
super(message);
this.name = this.constructor.name;
}
}
class CustomError extends Error {
constructor(message: string) {
super(message);
this.message = message;
this.name = "CustomError";
}
}

This rule is not configurable.

If you have a specific naming convention for errors that doesn’t follow the standard pattern, or if you’re working with legacy code that can’t be easily refactored, you may need to disable this rule.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.