Skip to content

errorUnnecessaryCaptureStackTraces

Reports unnecessary Error.captureStackTrace() calls in Error subclass constructors.

✅ This rule is included in the ts logical presets.

Calling Error.captureStackTrace() inside the constructor of a built-in Error subclass is unnecessary, since the Error constructor calls it automatically.

class MyError extends Error {
constructor() {
Error.captureStackTrace(this, MyError);
}
}
class MyError extends Error {
constructor() {
Error.captureStackTrace?.(this, MyError);
}
}
class MyError extends Error {
constructor() {
Error.captureStackTrace(this, this.constructor);
}
}
class MyError extends Error {
constructor() {
Error.captureStackTrace(this, new.target);
}
}

This rule is not configurable.

If you’re extending a custom error class that changes the default behavior to not capture the stack trace, you may need to disable this rule for that specific class.

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