Skip to main content

no-unnecessary-type-conversion

Disallow conversion idioms when they do not change the type or value of the expression.

🔧

Some problems reported by this rule are automatically fixable by the --fix ESLint command line option.

💡

Some problems reported by this rule are manually fixable by editor suggestions.

💭

This rule requires type information to run, which comes with performance tradeoffs.

JavaScript provides several commonly used idioms to convert values to a specific type:

  • Primitive coercion (e.g. Boolean(value), String(value)): using a built-in primitive function
  • String concatenation (e.g. value + ''): turning a value into a string
  • Unary coercion (e.g. +value, !!value): using a built-in operator
  • The .toString() method defined on many types

These conversions are unnecessary if the value is already of that type.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-unnecessary-type-conversion": "error"
}
});

Try this rule in the playground ↗

Examples

String('123');
'123'.toString();
'' + '123';
'123' + '';

Number(123);
+123;
~~123;

Boolean(true);
!!true;

BigInt(BigInt(1));

let str = '123';
str += '';
Open in Playground

Options

This rule is not configurable.

When Not To Use It

If you don't care about having no-op type conversions in your code, then you can turn off this rule. If you have types which are not accurate, then this rule might cause you to remove conversions that you actually do need.


Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.

See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.

Resources