Skip to content

Fixing TS issues in VS Code - Astro

New Course Coming Soon:

Get Really Good at Git

Sometimes I run into issues in VS Code when working in my Astro projects, I don’t know it it’s VS Code, TypeScript, Astro, more likely a combination of it all.

Sometimes errors are subtle / confusing, for example in one case I create a file and imported it, but I got a red line under the import saying Cannot find module ... or its corresponding type declarations

You can try opening the VS Code command palette and execute Developer: Restart Extension Host or Developer: Reload Window and more often than not, the error goes away.

Also try stopping npm run dev and restarting it.

Final try, close VS Code and reopen it.


In one case I created a collection but TS didn’t pick up the type of the posts correctly, and got “any”:

In this case I had to create a markdown file for my content collection first, corresponding to how I defined the collection in my content/config.ts in src/content/blog/test.md, then the error went away.

That was easy.

Other times it’s an import error. First of course check the path is correct.

If using @ imports, check tsconfig.json contains that and the file is saved.

{
  "extends": "astro/tsconfigs/strict",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@components/*": ["src/components/*"],
      "@layouts/*": ["src/layouts/*"],
      "@lib/*": ["src/lib/*"],
      "@data/*": ["src/data/*"],
      "@src/*": ["src/*"],
    }
  }
}
Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching May 21, 2024. Join the waiting list!
→ Get my JavaScript Beginner's Handbook
→ Read my JavaScript Tutorials on The Valley of Code
→ Read my TypeScript Tutorial on The Valley of Code
→ Read my Astro Tutorial on The Valley of Code

Here is how can I help you: