Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cnshenj opened this issue Oct 4, 2016 · 8 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@cnshenj
Copy link

cnshenj commented Oct 4, 2016

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

@cnshenj cnshenj changed the title Declaration files in parent directories should not be included Declaration files in parent directories should not be included when excluded Oct 4, 2016
@cnshenj
Copy link
Author

cnshenj commented Oct 4, 2016

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

@aluanhaddad
Copy link
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.

@aluanhaddad
Copy link
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. 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.

@aluanhaddad
Copy link
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

@mhegazy
Copy link
Contributor

mhegazy commented Oct 4, 2016

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.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 4, 2016
@cnshenj
Copy link
Author

cnshenj commented Oct 4, 2016

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'.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 4, 2016

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?

@cnshenj
Copy link
Author

cnshenj commented Oct 4, 2016

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

@cnshenj cnshenj closed this as completed Oct 4, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants