Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error Invalid type "boolean | undefined" of template literal expression @typescript-eslint/restrict-template-expressions

When using eslint's "restrict template expressions", how do you log a boolean variable? Also, if I wanted to disable the restriction for a line, how to do it?

const flag:boolean = true;

console.log("flag=" + flag);
console.log(`flag=${flag}`);

both generate this error:

error Invalid type "boolean | undefined" of template literal expression @typescript-eslint/restrict-template-expressions

like image 984
Robert La Ferla Avatar asked Oct 22 '25 07:10

Robert La Ferla


1 Answers

In your .eslintrc:

module.exports = {
  "rules": {
    "@typescript-eslint/restrict-template-expressions": "error"
  }
};

Ref: https://typescript-eslint.io/rules/restrict-template-expressions/

You can suppress the error for a line like this:

// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
console.log(`flag=${flag}`);

Just a little suggestion, if you are using typescript, maybe you should use const or let instead of var

like image 104
Denilson Anachury Avatar answered Oct 23 '25 21:10

Denilson Anachury