Skip to content

Don't include all @types/ automatically, only dependencies from package.json #11917

Closed
@ivogabe

Description

@ivogabe

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions