errorUnnecessaryCaptureStackTraces
Reports unnecessary Error.captureStackTrace() calls in Error subclass constructors.
✅ This rule is included in the tslogicalandlogicalStrictpresets.
Calling Error.captureStackTrace() inside the constructor of a built-in Error subclass is unnecessary, since the Error constructor calls it automatically.
Examples
Section titled “Examples”class MyError extends Error { constructor() { Error.captureStackTrace(this, MyError); }}class MyError extends Error { constructor() { super(); this.name = "MyError"; }}Optional Chaining
Section titled “Optional Chaining”class MyError extends Error { constructor() { Error.captureStackTrace?.(this, MyError); }}class MyError extends Error { constructor() { super(); this.name = "MyError"; }}Using this.constructor
Section titled “Using this.constructor”class MyError extends Error { constructor() { Error.captureStackTrace(this, this.constructor); }}class MyError extends Error { constructor() { super(); this.name = "MyError"; }}Using new.target
Section titled “Using new.target”class MyError extends Error { constructor() { Error.captureStackTrace(this, new.target); }}class MyError extends Error { constructor() { super(); this.name = "MyError"; }}Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.