Skip to content

Declaration files in parent directories should not be included when excluded #11363

Closed

Description

@cnshenj

TypeScript Version: 2.0.3

Directory structure

project
    - public (client stuff)
          - scripts
                - typings (declaration files installed by typings)
                - tsconfig.json (client project)
    - server
          - tsconfig.json (server project)
    - node_modules (contains some @types to be used by server project)

Expected behavior:
When compiling client project, which is under public/scripts directory, only files under public/scripts directory should be compiled and referenced.

Actual behavior:
Declaration files in node_modules/@types are referenced, causing compilation errors. The project itself is specified to use "amd" module system.
Adding following to tsconfig.json doesn't help

{
    "exclude": [
        "../../node_modules"
        "node_modules"
    ]
}

Examples of compilation error:

../../node_modules/@types/express/index.d.ts(16,30): error TS2307: Cannot find module 'serve-static'.
../../node_modules/@types/node/index.d.ts(73,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'require' must be of type 'Require', but here has type 'NodeRequire'.
../../node_modules/@types/passport/index.d.ts(28,30): error TS2307: Cannot find module 'express'.
../../node_modules/@types/serve-static/index.d.ts(16,20): error TS2307: Cannot find module 'mime'.

The issue only happens when compiling a project. Compiling files directly works fine, e.g. tsc -t es5 -m amd --jsx react --sourceMap main.ts

Activity

  1. changed the title [-]Declaration files in parent directories should not be included[/-] [+]Declaration files in parent directories should not be included when excluded[/+] on Oct 4, 2016
  2. cnshenj commented on Oct 4, 2016

    @cnshenj
    Author

    Also, declaration files under public/scripts/typings are not used, unless I explicitly add /// <reference path="../typings/index.d.ts" /> to source files.

  3. aluanhaddad commented on Oct 4, 2016

    @aluanhaddad
    Contributor

    I've run into this issue a number of times and I would really like to see a fix for it. Unfortunately there's some argument I believe as to whether or not it's a bug or a feature.

  4. aluanhaddad commented on Oct 4, 2016

    @aluanhaddad
    Contributor

    The reason the exclude doesn't work is because it's designed to work around the problem that while one (usually) does not want to compile TypeScript downloaded as part of external packages, one often wants their definitions, and this is essential for Types (@types). The broader issue is that the language service mimics the node module resolution algorithm. This performs a recursive walk up the directory tree looking for declaration files specifically in folders named node_modules/x and node_modules/@types/x where x is the exact string name of the imported dependency.

    This behavior is not amenable to RequireJS and SystemJS loaders and environments. Is also completely unhelpful for those using a package manager other than npm to manage their client sids dependencies.

    That said, this is not the reason why reason the declarations in your typings folder are not being picked up. You can specify "compilerOptions.typeRoots": ["./typings"] but it depends on additional factors. Posting your full tsconfig file would be helpful.

  5. aluanhaddad commented on Oct 4, 2016

    @aluanhaddad
    Contributor

    cnshenj my apologies it looks like there's a specific answer to solve your specific problem.
    Set "compilerOptions.types": [] see #11257.
    Sorry for the rant

  6. mhegazy commented on Oct 4, 2016

    @mhegazy
    Contributor

    If "types" is defined in tsconfig.json or --types is passed on the commandline, no automatic type inclusion is triggered. if you do not like what the compiler loads by default specify "types": [...] in you tsconfig.json.

  7. cnshenj commented on Oct 4, 2016

    @cnshenj
    Author

    types: [] does exclude node_modules as expected. But how do I include the typings directory in module resolution?

    I tried typeRoots: ["./typings"] but the compiler then looks for a "globals" name.

    tsc --traceResolution
    ======== Resolving type reference directive 'globals', containing file 'E:\crystal\public\scripts/__inferred type names__.ts', root directory 'E:/crystal/public/scripts/typings'. ========
    error TS2688: Cannot find type definition file for 'globals'.
    
  8. mhegazy commented on Oct 4, 2016

    @mhegazy
    Contributor

    But how do I include the typings directory in module

    i am not sure i understand the question. why including typings\index.d.ts in your project is not sufficient?

  9. cnshenj commented on Oct 4, 2016

    @cnshenj
    Author

    Sorry I misunderstood. Yes, types: [] is the solution.

  10. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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