17
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VSCodeの TypeScriptで、「Cannot redeclare block-scoped variable」が表示される

Last updated at Posted at 2019-09-21

VisualStudio Codeで TypeScriptを作った後、以下のコマンドなどでコンパイルをします。

tsc test.ts

すると、同じディレクトリーに .jsファイルができあがりますが、この時 VisualStudio Code上に図のようなエラーが表示されるようになります。

2019-09-21_11-54-30.png

Cannot redeclare block-scoped variable '...'

これは、コンパイル時の ECMAScriptのバージョンによるもののようで、「tsconfig.json」ファイルがあれば防ぐことができます。

tsconfig.json

tsconfig.json
{
  "compilerOptions": {
    /* Basic Options */
    // "incremental": true,                   /* Enable incremental compilation */
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
  }
}

先に、次のように初期化をしてから始めるとよさそうです。

tsc --init
17
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?