LoginSignup
1
2

More than 5 years have passed since last update.

Gulp+typescript+RequireJSで「Variable 'require' must be of type 'NodeRequire', but here has type 'Require'.」と怒られる問題に対応した

Posted at

現象

TypeScriptコードをコンパイルする際、以下のエラーが表示されてコンパイルできなかった。

/path/to/project/node_modules/@types/requirejs/index.d.ts(416,13): error S2403: Subsequent variable declarations must have the same type.  Variable 'require' must be of type 'NodeRequire', but here has type 'Require'.

結論

tsconfig.jsonに使用する型定義ファイルを明記することで解決した。

tsconfig.json
{
    "compilerOptions": {
        "types": [
            "requirejs"
        ]
    }
}

環境

関係するnpmパッケージ

  • "gulp": "^3.9.1"
  • "gulp-typescript": "^3.1.2"
  • "requirejs": "^2.3.2"
  • "typescript": "^2.0.6"

関係するファイル・ディレクトリ構造

project
├─node_modules
│  └─@types
│     ├─gulp
│     ├─gulp-typescript
│     ├─node
│     ├─orchestrator
│     ├─q
│     ├─requirejs
│     └─vinyl
├─src
│   └─ts
│      └─tsconfig.json
├─gulpfile.js
└─jsconfig.json

原因

typescript compilerが、@types以下の型定義ファイルを探してよしなにやってくれるらしい。
@types/gulpをインストールしたら@types/nodeも一緒にやってきてしまい、@types/requirejsとコンフリクトした。

雑感

  • この対応方法のままいくと、参照したい型定義ファイルが増えるたびに修正が必要なのでつらい
    • 使用する各ファイルでreference path書くよりはマシだろうけれど
  • excludeTypesとかあればうれしいのかしら

参考URL

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