flow-flow-flow

[TypeScript]TypeScriptプロジェクト始めようとしたらコンパイルで`Try changing the 'lib' compiler option to 'es2015' or later.`が出た

f:id:thegriftsense:20210808145535j:plain paizaでスキルチェックをしようと思ったけれど、言語選択でTypeScriptがなかった。 なので最小のTypeScriptプロジェクトを作成して、コンパイルしたJavaScriptのコードをコピペすればいいやと思ったので、作成手順をメモ。 またコンパイル時にTry changing the 'lib' compiler option to 'es2015' or later.というエラーがでたのでそのエラーの対応策もメモ。

package.json作成

$ npm init

typescript tsc を install

 $ npm i -D typescript tsc

tsconfig.json作成

$ ./node_modules/.bin/tsc --init

package.jsonにscript追加

コンパイルするときにTry changing the 'lib' compiler option to 'es2015' or later.が出ないように、--lib es6,domをつけてコンパイル時のライブラリを指定する。 -wwatchオプションで、付けておくとtsファイルの変更を監視してコンパイルしてくれる。

// src直下のtsファイルをes6を指定してコンパイル(監視)
"scripts":  {
  "tsc": "./node_modules/.bin/tsc ./src/**.ts -w --lib es6,dom"
}

src/index.tsを作成

コンパイルテスト

$ npm run tsc