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

Using /// <reference lib="dom" /> in a .d.ts typescript declaration file has global effects #33901

Closed
pyBlob opened this issue Oct 9, 2019 · 7 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@pyBlob
Copy link
Contributor

pyBlob commented Oct 9, 2019

TypeScript Version: 3.5.3, 3.6.3

Search Terms:

  • triple slash lib dom
  • triple slash global

Code

arbitrary.d.ts

/// <reference lib="dom" />

index.ts

export let x: Element;

Expected behavior:
Executing npx tsc --noEmit --target esnext --lib esnext index.ts should produce a compile error because Element is not defined nor imported and lib dom is not set globally.

The documentation for triple slash directives states the following:
https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html

Actual behavior:
It just compiles.

Related Issues:

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 9, 2019
@RyanCavanaugh
Copy link
Member

This is what a lib reference does.

@pyBlob
Copy link
Contributor Author

pyBlob commented Oct 11, 2019

Ah ok, so lib references also have this behaviour for all files in the compilation, not only the d.ts files. After reading the documentation another time I found the description: For example, adding /// <reference lib="es2017.string" /> to one of the files in a compilation is equivalent to compiling with --lib es2017.string..

We have found this is an issue, because we had a node typescript project that uses superagent/supertest. During refactoring an import to our own "Element" method got dropped, which however didn't cause the compilation to fail. Instead it caused a seperate project to fail compilation, because types didn't match. (Local testcases would have probably caught this issue).

Is there a way to make sure that no package may change our compilation settings? (other than manually checking every file and after every update)
Is there a way to have local lib references, that are scoped to the file they are used in?

@pyBlob
Copy link
Contributor Author

pyBlob commented Oct 11, 2019

Do you think that splitting @types packages like the one for superagent into multiple packages depending on intended usage environment is a viable option?

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@aleclarson
Copy link

aleclarson commented Nov 13, 2020

@RyanCavanaugh Should type directives be confined to the project they're imported by?

For example, let's say I have a __stories__ folder nested in my React Native app's src directory, Storybook happens to depend on @types/node, and I have a tsconfig.story.json that only includes **/__stories__ modules.

The problem is that Storybook uses a type directive, and it leaks into my React Native modules, despite them never importing Storybook. I've made sure the tsconfig.native.json (used by React Native modules) excludes **/__stories__ modules, yet @types/node leaks in anyway.

@carlpaten
Copy link

Is there a workaround for this from the point of view of the library user? type-fest and cross-fetch both include triple-slash references, and they both appear several times in our transitive dependencies. I'm happy to work with them to solve this problem, but it will take a lot of time and effort for the solution to trickle through the ecosystem. Is there something we can do in the meantime?

@aleclarson
Copy link

@LilRed There's no workaround until the strictEnvironment proposal or something similar is implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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

5 participants