Featured image of post 「Calls to 'console.error' are not allowed.」の解決方法【TypeScript】

「Calls to 'console.error' are not allowed.」の解決方法【TypeScript】

TypeScriptでconsole.errorなどを使うと出る「Calls to 'console.error' are not allowed.」の警告は、tslint.jsonのrulesに1行追加するだけで消せます。ログ出力にconsoleを使いたいときの解決方法です♪

406文字

consoleを使うと警告が出る

TypeScriptを使っている際に、ログ出力のためにconsole.error()などを使うと以下のような警告が出るようになります。

警告内容
WARNING in /Users/blogenist/Develop/sample/src/App.vue
106:9 Calls to 'console.error' are not allowed.
    104 |       })
    105 |       .catch(err => {
  > 106 |         console.error("Copy Error.", err);
        |         ^
    107 |       });
    108 |   }

これは、TypeScriptの環境ではconsole.error()を使用する事は許可されていないようです。

もちろん、別の記述に変えれば良いのですが使いたい場合はtslint.jsonに一行追加するだけで警告が出なくなるのでご紹介致します。

手順

tslint.jsonに追記

解決方法はとても簡単で、tslint.jsonrulesに以下を追加すれば使用しても警告が出なくなります。

src/tslint.json
{
  "defaultSeverity": "warning",
  "extends": ["tslint:recommended"],
  "linterOptions": {
    "exclude": ["node_modules/**"]
  },
  "rules": {
    "indent": [true, "spaces", 2],
    "interface-name": false,
    "no-consecutive-blank-lines": false,
    "object-literal-sort-keys": false,
    "ordered-imports": false,
    "quotemark": [true, "double"],
    "trailing-comma": [true, "never"],
    "curly": true,
    "arrow-parens": [2, "always"],
    "no-console": [false] // 追加
  }
}

とても簡単ですね!

終わりに

以上のように、原因さえ分かれば簡単に警告を消す事が出来ました。

お困りの際はぜひこのやり方を試してみてください♪