Skip to content

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

Description

@qz2017

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?

Activity

  1. octogonz commented on Nov 13, 2017

    @octogonz

    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.

  2. mhegazy commented on Nov 14, 2017

    @mhegazy
    Contributor

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

  3. octogonz commented on Nov 14, 2017

    @octogonz

    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?

    Ian Clanton-Thuon (@iclanton)

  4. mhegazy commented on Nov 14, 2017

    @mhegazy
    Contributor

    tsconfig.json is all you need

  5. typescript-bot commented on Nov 29, 2017

    @typescript-bot
    Contributor

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

  6. locked and limited conversation to collaborators on Jun 14, 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