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
class foo
foo
extends
var Error: ErrorConstructor
Error
{
constructor(
message: string
message
: string) {
super(
message: string
message
);
this.
Error.name: string
name
= "foo";
}
}
class
class CustomError
CustomError
extends
var Error: ErrorConstructor
Error
{
constructor(
message: string
message
: string) {
super(
message: string
message
);
}
}
class
class CustomError
CustomError
extends
var Error: ErrorConstructor
Error
{
constructor(
message: string
message
: string) {
super(
message: string
message
);
this.
Error.name: string
name
= this.
Object.constructor: Function

The initial value of Object.prototype.constructor is the standard built-in Object constructor.

constructor
.
Function.name: string

Returns the name of the function. Function names are read-only and can not be changed.

name
;
}
}
class
class CustomError
CustomError
extends
var Error: ErrorConstructor
Error
{
constructor(
message: string
message
: string) {
super(
message: string
message
);
this.
Error.message: string
message
=
message: string
message
;
this.
Error.name: string
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 ❤️‍🔥 around the world by the Flint team and contributors.