はじめに
本記事は、タイトルにある通り TypeScript のコンパイル時に以下のようなエラーが出たときの解決策です。
エラー内容
tscコマンドを実行して、TypeScript ファイルをコンパイルした際に、以下のようなエラーが発生しました。
% tsc using-ts.ts
../../../../node_modules/@types/node/globals.d.ts:341:13 - error TS2403: Subsequent variable declarations must have the same type. Variable 'AbortSignal' must be of type '{ new (): AbortSignal; prototype: AbortSignal; abort(reason?: any): AbortSignal; timeout(milliseconds: number): AbortSignal; }', but here has type '{ new (): AbortSignal; prototype: AbortSignal; }'.
341 declare var AbortSignal: {
~~~~~~~~~~~
../../../../../../usr/local/lib/node_modules/typescript/lib/lib.dom.d.ts:2071:13
2071 declare var AbortSignal: {
~~~~~~~~~~~
'AbortSignal' was also declared here.
Found 1 error in ../../../../node_modules/@types/node/globals.d.ts:341
環境によって内容は異なるかと思いますが、どうやらAbortSignal
の定義が重複してしまっていることが原因のようです。
上記の例だと、@types/node/globals.d.ts
とtypescript/lib/lib.dom.d.ts
が重複しているとのこと。僕の環境では、Node.js と TypeScript がどちらもグローバルでインストールされているようです。
ちなみにバージョンは、@types/node が 15.3.0、typescript は 4.9.4 でした。
解決方法
定義が重複してしまっているのが原因ですので、とりあえずどちらかを削除するのが良さそうです。
念の為、それぞれの定義をみてみます。
declare var AbortSignal: {
prototype: AbortSignal;
new(): AbortSignal;
};
declare var AbortSignal: {
prototype: AbortSignal;
new(): AbortSignal;
abort(reason?: any): AbortSignal;
timeout(milliseconds: number): AbortSignal;
};
今回は、TypeScript を使った作業に関するものなので、TypeScript (typescript/lib/lib.dom.d/ts) 側を残します。なので、@type/node 側の4行をコメントアウトしました。
// declare var AbortSignal: {
// prototype: AbortSignal;
// new(): AbortSignal;
// };
以上でコンパイル時のエラーは解消されました。
修正した場所を記録する目的も兼ねて記事にしておきました。