-
Notifications
You must be signed in to change notification settings - Fork 12.8k
jQuery type definitions are automatically included #22016
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
Comments
Is this a VS Code issue? If you run the compiler from the command line, does it compile successfully?
|
This is a TypeScript compiler issue. It is compiling successfully but I want the compiler to fail if I don't do the proper jQuery import. The code shouldn't magically assume that jQuery is defined, I want it to be explicitly imported (like the other libraries I'm using).
To my knowledge the compiler shouldn't be able to resolve Eventually I want to remove jQuery from my project, so I would like to keep track where jQuery is being used. |
I've created a small test application. It seems if I import jQuery in file A.ts, then it is also automatically available in file B.ts. Is that normal behavior? |
@Zyphrax it's not a compiler problem, it's the way the types in declare const jQuery: JQueryStatic;
declare const $: JQueryStatic; These tell the TypeScript compiler that It looks like Looks like other people are also wanting to know how to do this: #12473. |
Ideally it'd be written as a module and use |
Ah I see, thank you. I'm a bit confused though why you would ever want a global to apply to all .ts files. When I import something in file X, I expect it to only affect file X. Someone adding a class in the project could potentially change the way the rest of the project works, that sounds a bit scary. |
Yep, and it's not a recommended way to build a package, for sure, but that's actually how jQuery works. |
Would it perhaps be possible to have a compiler option to limit the scope of these globals? Or would that be a lot of work? For now I might have a look at this: https://www.npmjs.com/package/jquery-ts This makes me want to pull jQuery out of the project, but that is pushing it a bit for my next release 😄 |
It looks like (?) that package will still put the jQuery stuff in the globals, looking at it's source - it just doesn't report that in its type definitions. I'd personally not trust it, since if you accidentally include two conflicting jQuery versions, nothing will complain (when it probably should, since one will overwrite the other). The optimal solution is a custom jQuery build with |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.7.2
Search Terms: types,jquery,implicit,import
Code
test.ts
tsconfig.json
Expected behavior:
$ and jQuery should be undefined.
I expect to have to import
$
:Actual behavior:
The TypeScript compiler magically knows what $ and jQuery are without importing the types.
I don't want these types to be imported implicitly, that's why I'm setting
types
andtypesRoots
. But somehow the compiler does know what they are.I've tried disabling this behavior in all kinds of ways, but I can't seem to fix it.
My package.json (I've removed a few irrelevant dependencies):
I'm using the latest Visual Studio Code. My
.vscode/settings.json
instructs to use the bundled TypeScript version:{ "typescript.tsdk": "node_modules/typescript/lib" }
The text was updated successfully, but these errors were encountered: