2

VSCode editor supports Typescript module auto-import. For a library like lodash, it automatically and conviniently adds the following correct import, once sortBy is typed anywhere in the code:

import { sortBy } from "lodash-es";

However, I would like to import all lodash functions from their submodules, like that:

import sortBy from "lodash-es/sortBy";

Is it possible to somehow blacklist top level module "lodash-es", so that VSCode imports from submodule like "lodash-es/sortBy"?

I have a tslint rule https://palantir.github.io/tslint/rules/import-blacklist/ configured, so it prohibits top level import from "lodash-es" at lint time. But in the editor I still have to manually fix every autoimport.

1
  • Did you ever figure out a way to do this? Commented Apr 29, 2022 at 17:22

1 Answer 1

1

I looked at the TypeScript auto-import source code and I'm pretty sure there's currently no way to do this short of forking @types/lodash-es and restructuring it to remove the exports from the top-level module. I tried declaring my own lodash-es module with no exports that would shadow the real lodash-es top-level module, but that caused the type declaration for lodash-es/sortBy to generate an error. I'd encourage you to file a suggestion for the blacklisting.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for detailed (though unfortunate) answer and suggestion! Shouldn't this suggestion go into github.com/Microsoft/vscode/issues? Or they are using some language services being developed as part of github.com/Microsoft/TypeScript/issues project?
Indeed, VS Code uses the TypeScript language services for auto imports.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.