現象
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とかあればうれしいのかしら