LoginSignup
24
16

More than 5 years have passed since last update.

超初心者TypeScriptメモ

Last updated at Posted at 2019-01-04

超初心者TypeScriptメモ

環境

$ npm list -g typescript
`-- typescript@3.2.2
tsc --version
Version 3.2.2

TypeScript

tsconfig.jsonを作る

tsc --init
  • エラーが出る場合、グローバルのtypescriptとtscをuninstallして、typescriptだけ入れ直す
npm uninstall tsc -g
npm uninstall typescript -g
npm install typescript -g

コンパイルする

tsc hoge.ts

コンパイルする(lib指定)

tsc hoge.ts --lib es6,dom

コンパイル時に↓のようなエラー(例:TS2585)が出る場合等、libの指定が必要な時用
Try changing the lib compiler option to es2015 or later. 云々

node_modules/@types/zen-observable/index.d.ts:36:14 - error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

コンパイルする(tsconfig.jsonを使う)

tsc -p tsconfig.json
  • この時、lib等の指定はtsconfig.json内で行う
  • ファイルの指定がしたい場合はtsconfig.json内のfilesを指定。
  • ※↓みたいな感じ。
tsconfig.json内
{
    "compilerOptions": {
        /* ~略~ */
        "target": "es6",
        "module": "commonjs",
        "lib^": ["es6","dom"],
        /* ~略~ */
    },
    "files": ["hoge.ts"]
    }
}

コンパイルするとTS2580が出る(Do you need to install type definitions for node?)

↓のエラーが出る場合、

node_modules/aws-sdk/clients/firehose.d.ts:182:22 - error TS2580: Cannot find name 'Buffer'. Do you need to install type definitions for node? Try `npm i @types/node` and then add `node` to the types field in your tsconfig.

↓のnpm installを実行して、

npm i @types/node

tsconfig.jsonにtypesを追加する。

tsconfig.json
{
    "compilerOptions": {
        /* ~略~ */
        "types": ["node"],
        /* ~略~ */
    },
}

で、tsconfig.jsonを指定してコンパイルする。

tsc -p tsconfig.json
24
16
0

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
24
16