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)
In my case this solve the problem:
tsconfig.json
I had to add
*.astroto myincludes, now it works, thanks @Farnsi .OK, so…
I had this in my tsconfig.json:
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
includedirective 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
baseUrloption intsconfig.json.Instead of doing this:
Do this:
I also needed to add the
includeproperty to mytsconfig.jsonfile to get this working.For reference, here is my working
tsconfig.jsonfile: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
*.astrofiles. My tsconfig and itsincludearray 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.jsonfile with the line:"typescript.tsdk": "node_modules/typescript/lib". I have no idea why it would randomly switch to its version.Fix
*.tsfile (you must open a TypeScript file for the required menu to appear){}button in the bottom toolbar beside “TypeScript” and then click the “Select Version” linkIf you make a change, reload VSCode for it to take full effect and for it to reprocess all open tabs (including any
*.astrofiles 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.
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.1here is my
tsconfig.jsonSame problem here, solved it by adding “**/*.astro” in the include key of tsconfig.json
include : ["**/*.astro", OTHER FILES ]Same problem here, but only with
.astrofiles. With.tsall works as it should.Resolved by launching
Vs Code Command Palette > Developer: Restart Extension host.