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

Incorrect handling of importing types in script-setup #300

Closed
roamiiing opened this issue Jul 13, 2021 · 4 comments
Closed

Incorrect handling of importing types in script-setup #300

roamiiing opened this issue Jul 13, 2021 · 4 comments
Labels
duplicate This issue or pull request already exists

Comments

@roamiiing
Copy link

roamiiing commented Jul 13, 2021

App.vue:

<template>
  <div @click="count++">{{ count }}</div>
</template>

<script setup lang="ts">
import { ref, Ref } from 'vue';

const count: Ref<number> = ref(0);
</script>

I see that explicitly defining the Ref<number> type is not necessary, but this is just an example. VSCode shows an error in the import statement:

Screenshot 2021-07-13 at 19 34 06

Which is not actually an error because the imported type Ref is not being used as a value anywhere. This doesn't happen if we rewrite this code without using <script setup>:

<template>
  <div @click="count++">{{ count }}</div>
</template>

<script lang="ts">
import { defineComponent, ref, Ref } from 'vue';

export default defineComponent({
  setup() {
    const count: Ref<number> = ref(0);

    return {
      count,
    };
  },
});
</script>
@jods4
Copy link

jods4 commented Jul 13, 2021

This is a problem with the Vue compiler, the main issue about it is here:
vuejs/core#3183

Your code is correct, but the compiler exposes all imports (without knowing if they're a type or not, as there's not cross-file analysis going on here) to the template.
This puts Ref, a type, into a position where it can't be used, something like return { Ref } at the end of the setup function.

A consequence is that it's impossible to use const enums in script setup, see #277 as well.

@roamiiing
Copy link
Author

Well now I see what is going on with that, but I am curious why then am I not getting a runtime error

@jods4
Copy link

jods4 commented Jul 13, 2021

Prob. because your code doesn't use that value symbol.
If the export ends up with an unused, Ref property set to undefined, nothing bad is gonna happen.

I also believe that in production build, script setup is compiled differently, with a closure. This way doesn't cause any type to leak at all, either.

@johnsoncodehk
Copy link
Member

johnsoncodehk commented Jul 14, 2021

This is designed behavior to prevent runtime error. See #209 (comment).

螢幕截圖 2021-07-14 下午3 45 42

@johnsoncodehk johnsoncodehk added the duplicate This issue or pull request already exists label Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants