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
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);

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 ❤️‍🔥 around the world by the Flint team and contributors.