-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
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
@vue/compiler-sfc cannot build <script setup> components that use generic discriminated union props #8468
Comments
// props.ts
type Text = { text: string }
type Number = { number: number } sorry, I misread. |
🤔 no, that's not the intended props. I did in fact mean the discriminated union that was provided. |
Currently, Vue disallows dynamic props keys that from TS types. Because Vue's compiler needs to analyze TS types and transform them into the runtime definition of a specific prop, and it cannot be analyzed statically in build time. |
Ok, that actually makes a lot of sense @sxzz. Related: Has any consideration been giving to providing a mechanism to remove the distinction of props/attrs and treating all of them the same (as props)? This would be useful for library authors who would like to create dynamic prop APIs. To prevent breaking changes this could be an opt-in option on a per-component basis, similar to |
Hey there! Another use case. There are compiler errors, but Volar seems to work fine.
Code <template>
<slot v-bind="props" />
</template>
<script setup lang="ts" generic="TProps extends Record<string, unknown>">
const props = defineProps<TProps>()
defineSlots<{
default(props: TProps): JSX.Element
}>()
</script> Usage <script setup>
import LocalScope from './LocalScope.vue'
</script>
<template>
<LocalScope v-slot="{ test, lorem, ipsum }" :lorem="42" ipsum="dolor">
// ^ expected TS error
<span>{{ test }}</span>
<span>{{ lorem }}</span>
<span>{{ ipsum }}</span>
</LocalScope>
</template> |
@justin-schroeder Can you try this plz |
I was able to achieve my case using this implementation. LocalScope.vue <script setup lang="ts" generic="T">
defineOptions({ inheritAttrs: false })
defineProps</* @vue-ignore */ T>()
</script>
<template>
<slot v-bind="$attrs as T" />
</template> App.vue <script setup>
import LocalScope from './LocalScope.vue'
</script>
<template>
<LocalScope item-classes="rounded p-3 bg-blue-500" #default="{ itemClasses }">
<div :class="itemClasses">
No Data
</div>
<div :class="itemClasses">
Loading
</div>
<div :class="itemClasses">
Error
</div>
<div :class="itemClasses">
Data
</div>
</LocalScope>
</template> |
Vue version
3.3.4
Link to minimal reproduction
https://github.com/justin-schroeder/generics-discriminated-union-reproduction
Steps to reproduce
Any component with generics, where the generics extend a discriminated union cannot be compiled by
@vue/compiler-sfc
— however, they work just fine with Volar.What is expected?
Typed prop unions work both in both Volar and build time.
What is actually happening?
The following error is thrown:
Full reproduction repository here: https://github.com/justin-schroeder/generics-discriminated-union-reproduction
System Info
The text was updated successfully, but these errors were encountered: