Auto-import Typescript sub-mobule instead of parent module in VSCode

Viewed 401

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 Answers

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.

Related