3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

TypeScriptでコンパイル時にAbortSignalのエラー

Posted at

はじめに

本記事は、タイトルにある通り TypeScript のコンパイル時に以下のようなエラーが出たときの解決策です。

エラー内容

tscコマンドを実行して、TypeScript ファイルをコンパイルした際に、以下のようなエラーが発生しました。

tscコマンド実行
% 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.tstypescript/lib/lib.dom.d.tsが重複しているとのこと。僕の環境では、Node.js と TypeScript がどちらもグローバルでインストールされているようです。
ちなみにバージョンは、@types/node が 15.3.0、typescript は 4.9.4 でした。

解決方法

定義が重複してしまっているのが原因ですので、とりあえずどちらかを削除するのが良さそうです。
念の為、それぞれの定義をみてみます。

globals.d.ts(341行目)
declare var AbortSignal: {
    prototype: AbortSignal;
    new(): AbortSignal;
};
lib.dom.d.ts(2071行目)
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;
// };

以上でコンパイル時のエラーは解消されました。
修正した場所を記録する目的も兼ねて記事にしておきました。

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?