literalConstructorWrappers
Prefers literal syntax over constructor function calls for primitive values.
✅ This rule is included in the tsstylisticandstylisticStrictpresets.
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 const value: bigint
value = var BigInt: BigIntConstructor(value: bigint | boolean | number | string) => bigint
BigInt(123);const const value: boolean
value = var Boolean: BooleanConstructor<boolean>(value?: boolean | undefined) => boolean
Boolean(true);const const value: number
value = var Number: NumberConstructor(value?: any) => number
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number("42");const const value: string
value = var String: StringConstructor(value?: any) => string
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
String(123);const const value: string
value = var String: StringConstructor(value?: any) => string
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
String(true);const const value: 123n
value = 123n;const const value: true
value = true;const const value: 42
value = 42;const const text: "123"
text = "123";const const text: string
text = `${const someVariable: any
someVariable}`;const const value: bigint
value = var BigInt: BigIntConstructor(value: bigint | boolean | number | string) => bigint
BigInt(const variable: any
variable);const const value: boolean
value = var Boolean: BooleanConstructor<any>(value?: any) => boolean
Boolean(const variable: any
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