-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Auto import not working for lodash #29039
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
To add to this, it does work once you already have an existing import from lodash in the file. The suggestions don't show up in auto-complete, but it does give a quick fix suggest to add the new method to the existing import from lodash. |
This issue provides a repro: microsoft/vscode#67505 |
This issue is duplicate of #28773 wherein during completion we don't have knowledge of symbols not in the program and hence no offering in the completions. |
@sheetalkamat It's a little surprising that TypeScript doesn't anticipate that a package listed in |
Any update on this one? This is definitely something I get annoyed by on a near daily basis. I am just continually surprised that this feature which works so well for most other packages doesn't work for the single most popular js library: Either vscode is doing something wrong or lodash is but it seems really odd that this has been an issue for so long |
@tnrich Do you know of a library (that takes a similar approach for its |
@aleclarson I'm not sure how lodash's .d.ts files are defined. Could you elaborate more? Do lodash's types get pulled from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/lodash by default? |
@tnrich I dove into Removing that line is the first step. The problem is that it can't be removed, because The other step is to add an Not sure if this is an issue with TypeScript itself, or just |
Hmm thanks for diving in @aleclarson. I poked around as well but am not super familiar with typescript so felt a bit lost. I agree that Is there someone we can ask about this? Maybe one of the @Andy-MS @aj-r -- I couldn't find a list of maintainers but it looks like you both have committed a fair amount to the |
Yeah lodash is using the namespace 'hack' to support |
@aj-r is the namespace "hack" causing auto-import to fail? I'm not sure I understand why anyone would be mixing |
I don't know much about auto import so I can't say.
That import..require syntax is just how Typescript works - that was an old
way of importing commonjs packages before the esModuleInterop feature was
added.
…On Sun, Mar 10, 2019, 8:40 PM Thomas Rich, ***@***.***> wrote:
@aj-r <https://github.com/aj-r> is the namespace "hack" causing
auto-import to fail?
I'm not sure I understand why anyone would be mixing import and require().
That throws lint errors for me.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#29039 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGpes7jRFCwlPqWMTJjfPUQYfTvMKD75ks5vVaYGgaJpZM4ZUgAz>
.
|
Out of curiosity, does auto import work if you use lodash-es instead of
lodash? lodash-es is an ES module (and therefore does not use the namespace
hack), as opposed to normal lodash which is a commonjs module.
…On Sun, Mar 10, 2019, 8:40 PM Thomas Rich, ***@***.***> wrote:
@aj-r <https://github.com/aj-r> is the namespace "hack" causing
auto-import to fail?
I'm not sure I understand why anyone would be mixing import and require().
That throws lint errors for me.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#29039 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGpes7jRFCwlPqWMTJjfPUQYfTvMKD75ks5vVaYGgaJpZM4ZUgAz>
.
|
I just tried lodash-es @aj-r but auto-import doesn't seem to work for me in vscode. |
It seems like we're close. It probably has something to do with the .d.ts file. I wonder what needs to be changed to allow auto-import to work correctly.. |
While it would be nice if |
@aleclarson can you share a picture or video of lodash-es auto-import working for you? |
@aleclarson sweet thanks for that video! After installing It will unfortunately be somewhat complicated to switch over to using lodash-es in all my packages. I still think the better solution would be to somehow alter the lodash index.d.ts so that auto import works correctly. As long as |
@tnrich When When a user writes the following TypeScript: import { each } from 'lodash' ..it's equivalent to this JavaScript: const { each } = require('lodash') ..which is "destructuring" the CommonJS exports of the Auto-import can't suggest properties of a CommonJS namespace, because it can't know if the CommonJS package is meant to be used that way. That being said, you could try opening a new issue with a proposal for |
@aleclarson thanks for the detailed explanation. That makes sense to me. Where should I open that new issue? Would that be in the Typescript repo or in a vscode repo? |
Almost every comment in this thread is confused about something and it'd be an hour or more for me to set everything straight here. Please don't read this thread for information. The primary root cause of this issue is that the JS language service doesn't aggressively include types for packages it can't tell you're using yet, because doing so would be a fairly substantial perf hit for not a lot of gain. Compounding that is that ES6 destructuring imports from CommonJS modules is not a well-defined operation outside of loaders which ignore the spec to make that work. |
@RyanCavanaugh This comment shows that the actual problem is occurring for a package that is already in use, so what you're saying doesn't seem relevant? Nevertheless, I want to address your performance claim:
How often does the
Are you suggesting that |
Lodash auto import still doesn't work in vscode for me. @RyanCavanaugh could you please suggest some practical steps that we can take to get this fixed?
I don't understand why 80/90% of packages have no issues auto-importing into my project. Seems like lodash is doing something tricky that doesn't quite work. I'd love some more concrete tips that I could try to fix this issue. Thanks! |
To just fix the problem I do the following:
and maybe If you want some functionality from lodash that is not part of lodash es, like runInContext, install it under an alias:
|
@ricsam while your suggestion does work brilliantly, it is not compatible with npm. I've run into this issue with it when people try to use my packages:
https://stackoverflow.com/questions/54085943/npm-err-invalid-dependency-type-requested-alias |
Import in VSC still not working. I have to write _.something to find the method, then delete "_." and manually write import. |
From microsoft/vscode#63239
TypeScript Version: 3.3.0-dev.20181214
Search Terms:
Code
lodash
installed undernode_modules
kebabCase
Expected behavior:
Auto import suggestion to import from
lodash
returnedActual behavior:
No suggestion returned
The text was updated successfully, but these errors were encountered: