Help us understand the problem. What is going on with this article?

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

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
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away