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

Being aware of scoped slots types based on data from parent (typescript) #439

Closed
chojnicki opened this issue Aug 31, 2021 · 6 comments
Closed
Labels
question Further information is requested

Comments

@chojnicki
Copy link

Hi. Here's simple reproduction of scoped slots implementation (of course in this form it does not make any sense, it's just to show the problem).

DemoParent.vue

<template>
  <DemoChild
    :demo-prop="x" // value "a"
    v-slot="slotProps"
  >
    {{ slotProps.item }} <- will render "a" so should be same type as demo-prop
  </DemoChild>
</template>

<script setup lang="ts">
import DemoChild from '@/components/DemoChild.vue'

type DemoPropType = 'a' | 'b'
const x: DemoPropType = 'a'
</script>

DemoChild.vue

<template>
  <div>
    <slot :item="demoProp" />
  </div>
</template>

<script setup lang="ts">
defineProps({
  demoProp: {
    type: String,
    required: true,
  },
})
</script>

It's good that extension can detect in parent component type of slotProps based on types defined inside child component, so slotProps.item as string in this example. BUT in parent I'm passing not just any string but with certain type, so I need to know that returned slot prop is also in that type.

In real world example I have universal components for table and columns and it works just great on browser but in IDE I'm losing any information of passed data types which is bad.

Any ideas for workarounds or could this be supported by extension?

Regards!

@johnsoncodehk
Copy link
Member

Hi @chojnicki, in short vue can't support this for now. But you can do it with some hack ways by volar (I don't recommend it).

See:

@johnsoncodehk johnsoncodehk added the question Further information is requested label Aug 31, 2021
@chojnicki
Copy link
Author

chojnicki commented Sep 3, 2021

@johnsoncodehk This hacky workaround will not work in my case :( Event if this cant be automatically detected by Volar it would be great to be at least able assert type inside like v-slot="slotProps as someType" (I know it's Vue limitation not Volar in that matter).

But thanks anyway for detailed answer!

@johnsoncodehk
Copy link
Member

Let we wait for vuejs/core#1359 :)

@mesqueeb
Copy link
Contributor

@johnsoncodehk sorry to comment on a closed issue.
However, the vueJS issue you linked was already closed, so I was hoping for some clarification.

My parent has the scope it passes to the slot typed correctly:
image

but this is still not recognised by the child:
image

my feature request would be:

  • allow for auto-detecting the slot scope type based on what the parent is passing into the slot scope.

Should I create a new FR issue for this?

Best regards.

@johnsoncodehk
Copy link
Member

@johnsoncodehk
Copy link
Member

Just for record, this feature is back at v0.36.0 (only for script setup): https://github.com/johnsoncodehk/volar/blob/master/CHANGELOG.md#0360-202263

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants