-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Node module dependencies that use a triple-slash directive to reference libs poison the global scope #33111
Comments
FWIW I don’t think |
@fatcerberus Yes, that's right, I started to comment and realized I wasn't quite clear with what my option B meant. I edited it to this:
It's removing the triple slash directive that was missing from my description. The dependency will build fine ( |
If it was implemented and library authors followed suit, #31894 would handle this well. |
@rbuckton hadn't you considered option (2), creating standalone versions of the built-in lib files? |
Do you have and updates on this? I don't know how prevalent this problem is in other packages, but we're butting up against it in |
TypeScript Version: 2.x, 3.x
Search Terms: lib.dom.d.ts pollutes global scope,
name
is defined as never,event
is defined asEvent | undefined
See: https://github.com/AaronFriel/is-test/blob/4e235decffdfd3c137ea066f8462d57d2fc8d0f2/src/index.ts at the commit hash linked.
Expected behavior:
Given a
tsconfig.json
withlib: []
, I would expect the following code to fail to build due to$variable not defined
errors:Actual behavior:
See: https://github.com/AaronFriel/is-test/blob/master/src/index.ts for a particularly egregious example of this. It builds without any type errors. When running
npm run start
, it will throw a runtime error on the firstconsole.log
. If you remove that line, the next one fails, and so on, for the next 700+ lines.General purpose libraries often need to be written to handle multiple versions of ES (ES5, ES6, ES7...) and multiple host environments (e.g.: DOM, ScriptHost, WebWorker). But right now they are faced with a choice:
A: Use a triple slash directive to import types that they use, and poison the user's global scope, causing nearly one thousand variable names to be silently accepted as defined without any type checker errors.
B: Remove the triple slash directives and require that users use the
--skipLibCheck
option or equivalent in their config file or include the required libs in their compiler options. Otherwise the library will fail to type check.This is a choice that library authors would quite reasonably not want to make.
Potential solutions:
Triple slash directives should import types only in dependencies.
This seems tricky to implement, I think.
There should be a types-only version of the major lib files, so that general-purpose utility packages can reference those instead.
This seems less tricky: extract all the types from the
dom
lib a newdom-types
lib, and use a directive to reference the types. Then, library authors can triple-slash referencedom-types
and get only the types. Has the advantage of not causing any compatibility issues, and library authors can opt in to the types-only package.???
The text was updated successfully, but these errors were encountered: