literalConstructorWrappers
Prefers literal syntax over constructor function calls for primitive values.
✅ This rule is included in the ts stylistic 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.
Examples
Section titled “Examples”const value = BigInt(123);const value = Boolean(true);const value = Number("42");const value = String(123);const value = String(true);const value = 123n;const value = true;const value = 42;const text = "123";const text = `${someVariable}`;const value = BigInt(variable);const value = Boolean(variable);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”- MDN documentation on BigInt
- MDN documentation on Boolean
- MDN documentation on Number
- MDN documentation on String
Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
unicorn/prefer-bigint-literals