withastro / language-tools

Language tools for Astro
MIT License
270 stars 54 forks source link

πŸ› BUG: Support for typescript inside <script> tags with define:vars #755

Closed calvinrbnspiess closed 10 months ago

calvinrbnspiess commented 10 months ago

Describe the Bug

Hello! I'm currently experiencing the following issue. When using the astro language tools inside VS Code, I'm getting typescript errors like "Type assertion expressions can only be used in TypeScript files.ts(8016)".

Generally the typ checking is working flawlessly, however this odd error appears when I'm adding the define:vars property.

The lack for define:vars is mentioned inside this pull request: https://github.com/withastro/language-tools/pull/263

Is this behaviour still intentional? Are there any workarounds till this gets fixed?

This is working:

Bildschirmfoto 2024-01-11 um 16 25 57

This is not working:

Bildschirmfoto 2024-01-11 um 16 25 40

My tscconfig.json:

{
  "extends": "astro/tsconfigs/strict",
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "react"
  }
}

Extension (astro-build.astro-vscode) version: v2.6.2 VS Code version: 1.85.1 (Universal)

    "astro": "^4.0.2",
    "@astrojs/check": "^0.3.1",
    "@astrojs/react": "^3.0.9",
    "@astrojs/tailwind": "^5.0.3",
    "typescript": "^5.3.2"

Steps to Reproduce

  1. Write typescript annotations inside a script tag in an astro codebase.
  2. Add define:vars={{ title: "test " }}
calvinrbnspiess commented 10 months ago

After further reading I discovered, that is behaviour is intentional. Whenever someone defines the property define:vars on a script tag, it will inherit the is:inline directive as stated in the big yellow warning on this docs page: https://docs.astro.build/en/reference/directives-reference/#definevars

Inline script won't be bundled and won't be processed by typescript, sadly.

sommerper commented 2 months ago

Would be nice if that could get added. I've had to remove the types so many times now because I at some point realize that I need define:vars.

At this point I'm considering to just not use typescript. But I would honestly prefer that typescript would be transpiled also when it inline.