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

Triple-slash reference affects all files instead of just the one file that contains it. #19978

Closed
qz2017 opened this issue Nov 13, 2017 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@qz2017
Copy link

qz2017 commented Nov 13, 2017

TypeScript Version: 2.4.2

Code

/// <reference types="mocha" />

Expected behavior:
Types from mocha are only available to the test ts file that include this triple-slash reference.

Actual behavior:
Types from mocha are available to all the ts files in the project, including both prod code and test code.
This is unexpected. How do we prevent Mocha test types from accidentally getting into prod code?

@octogonz
Copy link

We're having the same problem with Jest, which declares even more ambient definitions than mocha: If one unit test does /// <reference types="mocha" />, then all the product code can suddenly access these definitions.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 14, 2017

/// <reference types="mocha" /> is a global directive. it is an alias to --types in your tsconfig.json or on the commandline.

What mocha does is it adds declarations to the global scope, and there is no way to tease out what a global means in one module and not the other..

My recommendation here is to have your tests in a separate compilation, and they would include mocha.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 14, 2017
@octogonz
Copy link

octogonz commented Nov 14, 2017

My recommendation here is to have your tests in a separate compilation, and they would include mocha.

If we want VS Code IntelliSense for the unit tests, does that mean we need to create a separate package.json and/or tsconfig.json file?

@iclanton

@mhegazy
Copy link
Contributor

mhegazy commented Nov 14, 2017

tsconfig.json is all you need

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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

4 participants