😎

【豆知識】tscのコンパイルエラー<なぜか個別tscに失敗>

に公開

単純処理をtscでコンパイルしようとしたらエラーが出た

原因

個別ファイルに対してtscコンパイルするとtsconfig.jsonの設定が反映されないとのこと。
なのでファイル全体に対してtscするか、個別ファイルにtscしたいなら実行コマンドにオプションをつける。

サンプル例(src/add.ts)
export const add = (a: number, b: number): number => a + b;
個別ファイルに対してtscした実行結果
$ npx tsc src/add.ts
node_modules/@types/chai/index.d.ts:882:42 - error TS2552: Cannot find name 'ReadonlySet'. Did you mean 'Readonly'?

882                 haystack: readonly T[] | ReadonlySet<T> | ReadonlyMap<any, T>,
                                             ~~~~~~~~~~~

node_modules/@types/chai/index.d.ts:882:59 - error TS2552: Cannot find name 'ReadonlyMap'. Did you mean 'Readonly'?

882                 haystack: readonly T[] | ReadonlySet<T> | ReadonlyMap<any, T>,
                                                              ~~~~~~~~~~~

node_modules/@types/chai/index.d.ts:895:49 - error TS2583: Cannot find name 'WeakSet'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

895             include<T extends object>(haystack: WeakSet<T>, needle: T, message?: string): void;
                                                    ~~~~~~~

node_modules/@types/chai/index.d.ts:925:42 - error TS2552: Cannot find name 'ReadonlySet'. Did you mean 'Readonly'?

925                 haystack: readonly T[] | ReadonlySet<T> | ReadonlyMap<any, T>,
                                             ~~~~~~~~~~~

node_modules/@types/chai/index.d.ts:925:59 - error TS2552: Cannot find name 'ReadonlyMap'. Did you mean 'Readonly'?

925                 haystack: readonly T[] | ReadonlySet<T> | ReadonlyMap<any, T>,
                                                              ~~~~~~~~~~~

node_modules/@types/chai/index.d.ts:938:52 - error TS2583: Cannot find name 'WeakSet'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

938             notInclude<T extends object>(haystack: WeakSet<T>, needle: T, message?: string): void;
                                                       ~~~~~~~

node_modules/@types/chai/index.d.ts:970:42 - error TS2552: Cannot find name 'ReadonlySet'. Did you mean 'Readonly'?

970                 haystack: readonly T[] | ReadonlySet<T> | ReadonlyMap<any, T>,
                                             ~~~~~~~~~~~

node_modules/@types/chai/index.d.ts:970:59 - error TS2552: Cannot find name 'ReadonlyMap'. Did you mean 'Readonly'?

970                 haystack: readonly T[] | ReadonlySet<T> | ReadonlyMap<any, T>,
                                                              ~~~~~~~~~~~

node_modules/@types/chai/index.d.ts:983:59 - error TS2583: Cannot find name 'WeakSet'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

983             deepInclude<T>(haystack: T, needle: T extends WeakSet<any> ? never : Partial<T>, message?: string): void;
                                                              ~~~~~~~

node_modules/@types/chai/index.d.ts:1005:42 - error TS2304: Cannot find name 'ReadonlySet'.

1005                 haystack: readonly T[] | ReadonlySet<T> | ReadonlyMap<any, T>,
                                              ~~~~~~~~~~~

node_modules/@types/chai/index.d.ts:1005:59 - error TS2304: Cannot find name 'ReadonlyMap'.

1005                 haystack: readonly T[] | ReadonlySet<T> | ReadonlyMap<any, T>,
                                                               ~~~~~~~~~~~

node_modules/@types/chai/index.d.ts:1018:62 - error TS2583: Cannot find name 'WeakSet'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

1018             notDeepInclude<T>(haystack: T, needle: T extends WeakSet<any> ? never : Partial<T>, message?: string): void;
                                                                  ~~~~~~~

node_modules/@types/deep-eql/index.d.ts:5:30 - error TS2583: Cannot find name 'WeakMap'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

5     class MemoizeMap extends WeakMap<object, MemoizeMap | boolean> {}
                               ~~~~~~~


Found 13 errors in 2 files.

Errors  Files
    12  node_modules/@types/chai/index.d.ts:882
     1  node_modules/@types/deep-eql/index.d.ts:5

上記の例だとReadonlySetやWeakSetというES2015で登場した機能が使えないとのこと。
tscが通るようにするには以下のオプション(lib配列にES2015を追加)を付ける

$ npx tsc src/add.ts --lib ES2015

参考: console.logを使いたい場合はlib配列にDOMを追加

$ npx tsc src/add.ts --lib ES2015,DOM

Discussion