Pattern: Error constructor without new
keyword
Issue: -
While errors can be created without the new
keyword, using it makes the constructor call explicit and clearer. Always use new
when throwing error objects.
Example of incorrect code:
throw Error("🦄");
throw TypeError("unicorn");
throw lib.TypeError("unicorn");
Example of correct code:
throw new Error("🦄");
throw new TypeError("unicorn");
throw new lib.TypeError("unicorn");