Description
The compiler currently picks all type declarations from node_modules/@types
. This causes that when a dependency adds some @types
package, this is automatically added to the compilation. However, this is not desired if the dependency is not used, for instance since it is only used in the build process. This could cause a lot of issues; imagine that a dependency of a dependency (etc.) adds an @types
dependency.
Real world example: gulp-typescript is now using @types
for its dependencies; @types/node
is now a dependency of gulp-typescript. This caused that @types/node
is now included in the projects of users of gulp-typescript, and TypeScript will automatically find this.
Currently users can either include all @types
(default) or include a specific list (by setting "types": [ ... ]
. My suggestion is something between those options:
Suggestion
Instead of picking all files of node_modules/@types
, first check if a package.json
file exist in the current directory or a parent directory. If so, read that file and include all @types
that are registered as a dependency or devDependency. If the package.json
file does not exist, fall back to the current behaviour.
If a project does include gulp-typescript for instance, the compiler will read the following line:
/// <reference types="node" />
The compiler will automatically include this when run with the declaration
option. So, types will only be added if they are really used either by the project itself, or by one of the dependencies (recursively).
I think that this would solve a lot of issues and make it easier for projects to migrate to @types
. What do you guys think?