Skip to content

literalConstructorWrappers

Prefers literal syntax over constructor function calls for primitive values.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

JavaScript provides built-in constructor functions for creating primitive values: BigInt(), Boolean(), Number(), and String(). While these constructors can coerce values to their respective types, using literal syntax is often more concise and idiomatic when the argument is already a literal value.

This rule flags constructor calls with literal arguments that could be replaced with literal syntax.

const value = BigInt(123);
const value = Boolean(true);
const value = Number("42");
const value = String(123);
const value = String(true);

This rule is not configurable.

If you prefer the explicit constructor syntax for consistency in your codebase, or if you’re maintaining a codebase where constructor calls are used as a deliberate style choice to make type coercion more explicit, you might prefer to disable this rule.

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