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

type definitions can disappear if they are mentioned in a template #5414

Closed
magicseth opened this issue Feb 12, 2022 · 2 comments
Closed

type definitions can disappear if they are mentioned in a template #5414

magicseth opened this issue Feb 12, 2022 · 2 comments
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. has workaround A workaround has been found to avoid the problem 🐞 bug Something isn't working scope: sfc

Comments

@magicseth
Copy link

Version

3.2.30

Reproduction link

stackblitz.com

Steps to reproduce

define a custom type in another file:

TestCase.ts:

export type TestCase = number;

Reference it in your component template as an Array or as TestCase[]:

App.vue:

<script setup lang="ts">
import { TestCase } from './TestCase';
let testArray = [];
</script>

<template>
  Hello World
  {{testArray as TestCase[]}}
</template>

What is expected?

It is expected that the type be in the generated js file for TestCase

What is actually happening?

the type is omitted ONLY IF you cast something as an array of TestCase in the template


This is a very bizarre bug where adding a bit of code in the template changes the javascript generated. It doesn't work with yarn dev, but works fine with yarn build.

@yyx990803 yyx990803 added 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. scope: sfc 🐞 bug Something isn't working has workaround A workaround has been found to avoid the problem labels Feb 12, 2022
@yyx990803
Copy link
Member

Vue checks whether an import is used in the template to determine whether it should be treated as a type-only import - but this leads to false positives when they are used as types in template expressions.

The workaround is using explicit import type { ... } from './...'.

@magicseth
Copy link
Author

Thanks Evan, that workaround works.

This caused me hours of head scratching in particular because my code still work with "yarn dev" and only broke when I tried "yarn build"

Is this something that tooling could take care of with automatically adding "type" to the import statement or a reference to this possibility in the error message?

For searchability here is the error I received when this happened:
SyntaxError: The requested module '/src/components/TestCase.ts' does not provide an export named 'TestCase'

client.ts:35 [hmr] Failed to reload /src/components/TestCase.vue. This could be due to syntax errors or importing non-existent modules. (see errors above)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. has workaround A workaround has been found to avoid the problem 🐞 bug Something isn't working scope: sfc
Projects
None yet
Development

No branches or pull requests

2 participants