language-tools: Import aliases don't work with typescript in .astro files in vscode.

Astro info

Astro version            v2.10.14
Package manager          yarn
Platform                 linux
Architecture             x64
Adapter                  Couldn't determine.
Integrations             None or couldn't determine.

What browser are you using?

Not relevant

Describe the Bug

When running yarn create astro selecting a strict typescript project with sample files, then following the guide to enable import aliases, import alias don’t work in .astro files.

tsconfig.json:

{
  "extends": "astro/tsconfigs/strict",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@components/*": ["src/components/*"],
      "@layouts/*": ["src/layouts/*"]
    }
  }
}

src/pages/index.astro

import Card from '@components/Card.astro';

Doesn’t work in VSCode. It compiles fine, but there’s no types since ts can’t find the definition. Aliases also seem to work fine from .ts/.tsx files.

What’s the expected result?

import Card from '@components/Card.astro'; should result in a typed Card import.

Link to Minimal Reproducible Example

Minimal reproducible example is described above. Requires VSCode.

Participation

  • I am willing to submit a pull request for this issue.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 34 (9 by maintainers)

Most upvoted comments

In my case this solve the problem:

tsconfig.json

{
  "extends": "astro/tsconfigs/strict",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@components/*": ["src/components/*"],
      "@layouts/*": ["src/layouts/*"],
    }
  },
  "include": [
    "src/**/*.d.ts",
    "src/**/*.ts",
    "src/**/*.astro"
  ]
}

I had to add *.astro to my includes, now it works, thanks @Farnsi .

{
  "extends": "astro/tsconfigs/strict",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  },
  "include": [
    "src/**/*.d.ts",
    "src/**/*.ts",
    "src/**/*.astro"
  ]
}

OK, so…

I had this in my tsconfig.json:

  "include": [
    "src/types.d.ts",
  ],

This is what the support AI in Astro’s Discord channel suggested adding, along with the actual types.d.ts in src/. I removed this line, and the types.d.ts file, and now aliases are resolving correctly with no errors.

However, I only added this line yesterday. Before that, if I recall, I had a value of * in there, and then had some excludes set (node_modules etc). This in turn was a remnant of trying to fix the problem using StackExchange some weeks ago.

Seems like having that include directive is the problem, at least for me. I wonder if something like this is happening with @miklschmidt?

But here’s what’s even weirder:

Before I fixed this, I also opened the (then-broken) project in Webstorm. And it resolved those aliases just fine using the same (apparently broken) tsconfig.json. Wth?

My problem was fixed by simply adding a baseUrl option in tsconfig.json.

Instead of doing this:

{
  "paths": { "@/*": ["./src/*"] }
}

Do this:

{
  "baseUrl": "src",
  "paths": { "@/*": ["*"] }
}

I also needed to add the include property to my tsconfig.json file to get this working.

For reference, here is my working tsconfig.json file:

{
  "exclude": ["node_modules"],
  "include": [
    "src/**/*.d.ts",
    "src/**/*.ts",
    "src/**/*.astro"
  ],
  "extends": "astro/tsconfigs/strictest",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "#assets/*": ["src/assets/*"],
      "#auth/*": ["src/auth/*"],
      "#components/*": ["src/components/*"],
      "#database/*": ["db/*"],
      "#layouts/*": ["src/layouts/*"]
    }
  }
}

Glad you figured it out, weird issue!

Since Astro 3.0 drops support for TypeScript 4 completely, I’ll close this issue, as everything should be working on any TypeScript 5.x version.

If this is still affecting some of you, feel free to create a new issue with new details.

I had this happen just now in an Astro project where import aliases were previously working in *.astro files. My tsconfig and its include array was in-line with the other commenters who have it working.

TL;DR if your tsconfig agrees with the others and you still have this issue, check which version of TypeScript is being used by VSCode

Cause

For whatever reason VSCode decided to use its own version of TypeScript instead of the project version, despite the project including a .vscode/settings.json file with the line: "typescript.tsdk": "node_modules/typescript/lib". I have no idea why it would randomly switch to its version.

Fix

  • open a *.ts file (you must open a TypeScript file for the required menu to appear)
  • hit the {} button in the bottom toolbar beside “TypeScript” and then click the “Select Version” link
  • make sure you have “Use Workspace Version” selected (even if the version number is the same as VSCode’s version)

If you make a change, reload VSCode for it to take full effect and for it to reprocess all open tabs (including any *.astro files you may have open). I’ve found the most reliable way for a change like this to take effect is to fully exit and then reopen VSCode.

At least for me, the issue was resolved… once again…

01/23/2024

If anyone is checking this in 2024, Yes the issue is still there.

With Astro v4.1.2 | 5.3.2TypeScript Version

The only viable working solution is to include this in your tsconfig.json

"include": ["src/**/*.d.ts", "src/**/*.ts", "src/**/*.astro"]

Which I think needs to be added to the docs.

Note: You need to restart the dev server to say changes takes effect (In my case that was necessary)

Thanks to @stav @Farnsi for sharing this

For me the issue is that I did not provide a baseUrl. baseUrl: '.', did the trick.

Problem still appearing on astro 5.1.1

here is my tsconfig.json

{
  "extends": "astro/tsconfigs/strict",
  "exclude": ["dist"],
  "compilerOptions": {
    "jsx": "preserve",
    "baseUrl": ".",
    "paths": {
      "@components": ["src/components/*"]
    }
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.d.ts",
    "src/**/*.astro",
    ".astro/types.d.ts",
    "**/*.astro",
    "**/*"
  ]
}

image

Same problem here, solved it by adding “**/*.astro” in the include key of tsconfig.json

include : ["**/*.astro", OTHER FILES ]

Same problem here, but only with .astro files. With .ts all works as it should.

Resolved by launching Vs Code Command Palette > Developer: Restart Extension host.

// tsconfig.json
{
  "extends": "astro/tsconfigs/strict",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  }
}