Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing types into Vue's SFCs doesn't work when using Pug. #5678

Closed
7 tasks done
mareszhar opened this issue Nov 15, 2021 · 3 comments
Closed
7 tasks done

Importing types into Vue's SFCs doesn't work when using Pug. #5678

mareszhar opened this issue Nov 15, 2021 · 3 comments
Labels
bug: upstream Bug in a dependency of Vite

Comments

@mareszhar
Copy link

Describe the bug

Note: I saw that it's recommended to report issues inside Vue's SFC in the vue/next repo, but I only face this issue when I use Vite and not when I use the Vue CLI, so that's why I decided to report it here.


Using an imported type inside a Vue component makes Vite's dev server render a completely blank page.

<script setup lang="ts">
import { someTypeScriptInterface } from "./types"
import { ref } from "vue"

// The line below will cause a fail even if the value passed to ref matches the Ref's typing:
const myRef = ref<someTypeScriptInterface>({ ... })
</script>

But, interestingly, the error only occurs if I'm using Pug in the component's template!

Setting the component's template back to HTML fixes the issue.

I should also note that:

  • My IDE does not complain. Using an imported type in a SFC works fine with the linter and I get full type inference support.
  • If I run npm run serve or npm run build instead of npm run dev, the app is compiled without any issues and works perfectly if I check it on the local development server.
  • The problem only occurs with npm run dev, preventing me from using Vite's HMR.
  • If I build the same project using Vue's CLI instead of Vite, I can use Pug in the template and type imports in the script tag. The page doesn't go blank and HMR still works.

I've been loving Pug and Vite, so I hope I don't have to give up on any of them. Any help fixing this bug or correcting a mistake on my end is extremely appreciated. Thank you ❤️ !

Reproduction

You can clone this minimal reproduction repo to reproduce the problem.

System Info

System:
  OS: Windows 10 10.0.19042
  CPU: (8) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
  Memory: 1.61 GB / 7.86 GB
Binaries:
  Node: 16.13.0 - C:\Program Files\nodejs\node.EXE
  npm: 8.1.0 - C:\Program Files\nodejs\npm.CMD
Browsers:
  Edge: Spartan (44.19041.1266.0), Chromium (95.0.1020.53)
  Internet Explorer: 11.0.19041.1202

Used Package Manager

npm

Logs

No response

Validations

@Shinigami92
Copy link
Member

Could you try import type { someTypeScriptInterface } from "./types" instead?

@mareszhar
Copy link
Author

@Shinigami92 Yes! Thank you!! That fixes the problem in my example and most uses of imported types in Vue ❤️

I've noticed, however, that it doesn't work with defineProps:

import type { User } from "./types"

defineProps<User>() // this line will cause a crash

When I do that, I get the error type argument passed to defineProps() must be a literal type, or a reference to an interface or literal type. But, the error doesn't occur if I define the exact same interface in place:

interface User { ... } // define user interface inside SFC

defineProps<User>() // this works! (???)

Maybe this is a bug with Vue's compiler or am I perhaps missing another keyword to correctly use the imported interface with defineProps?

Thank you so much for your support!

@Niputi
Copy link
Contributor

Niputi commented Nov 22, 2021

vuejs/core#4982
vuejs/core#4294 (comment)

@Niputi Niputi closed this as completed Nov 22, 2021
@Niputi Niputi added the bug: upstream Bug in a dependency of Vite label Nov 22, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Dec 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite
Projects
None yet
Development

No branches or pull requests

4 participants