Automatic typescript imports in VSCode are a life-saver and like all good things it hurts when they go away.
I keep seeing this problem in new projects where the automatic imports mostly work fine… except for new files 😡.
Here’s the infuriating workflow:
- Add new file
- Type
const MyComponent: FC
. Get a prompt to importFC
fromreact
. Do it. - Add the rest of the line:
const MyComponent: FC = () => <ExistingComponent />
- No auto import for
ExistingComponent
😢 - Manually add the import
- Now future auto imports work fine
In fact, if you manually import any component (excluding those from node_modules
) then auto-import will start working for new components.
import { IrrelevantComponent } from './SomewhereElse';
const MyComponent: FC = () => <ExistingComponent />; // <-- now prompted for auto import
The Fix
As ever with these kinds of things, the fix is a tiny change: update your tsconfig.json
to include files with a glob istead of root folder.
{
- "include": ["src"]
+ "include": ["src/**/*"]
}
You’re welcome, future-me.