-
-
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
defineSlots for script setup #4092
Comments
A small post-scriptum: How the component itself is declared seems irrelevant, so that probably means a |
I don't know since when, but today I noticed that Volar is able to type slots that are inside components defined by That's super cool and a great QoL improvement ✨ It would be super-duper awesome if there was a way to couple that Volar inferrence with a way to make props/slots generic. |
I'm curious what is the real benefit of type checking generated code here - |
Yes, I agree. I opened this when my slot was implicitly typed as There's no need for manually declaring A few extra thoughts / considerations(1) I'm building with TS not Esbuild, mainly because of const enums. Speed is always good but for production build is not the top priority. Doing 2 TS passes (one with (2) I am curious how this will all turn out with generic components, which would be a complex, albeit much needed, addition. Consider a kind of Form component, that injects a <template>
<form>
<slot :val="val" />
</form>
</template>
<script setup>
// Not sure what the syntax in script setup would be...
defineGeneric<M extends object, R extends Rules<M> | undefined>();
defineProps<{ model: M, rules?: R }>();
// This is injected in slot, its type should be Validation if rules props is not undefined, otherwise it's undefined.
const val: R extends Rules<M> ? Validation<M, R> : undefined;
</script> It can all be modelled by TS type system, but when you use the component in a template, I have no idea if it would even be possible to infer the right types! <!--
Here we need to infer:
M: { name: string }
R: { name: required }
So TS can then:
- Validate R satisfies Rules<M>
- Type val as `Validation<M, R>` (not undefined)
-->
<my-form :model="{ name: 'first' }" :rules="{ name: required }" v-slot="{ val }" />
<!--
This time we should infer:
R: undefined
Then val is `undefined` and trying to use it will result in a compilation error.
-->
<my-form :model="{ name: 'first' }" v-slot="{ val }" /> |
If the remaining utility is only related to generic components, I think it would be better discussed as part of the generic components proposal. So closing this one here. |
@yyx990803 如果使用volar开发,现在项目中能有scopedSlots props的类型提示,但是貌似是结合模版解析提示的。 但是如果构建一个第三方组件库, 所以在现有的defineComponents里加入slots类型变量,以及 或者可以在现有的 const slots = useSlots<Slots>(); |
This should be reopened. I use the latest Volar yet I do not have any type interference for slot props. I get type |
I also find this feature useful when I have slots in child components and would like to define them in the Enclosing Component. I don't know any solution for this. |
Done in #7982 |
What problem does this feature solve?
(I'm sure I saw this mentioned somewhere before, but I can't find any issue nor discussion about it. Apologies if it's a dupe.)
For Typescript users,
script setup
needs adefineSlots
macro to create strongly typed slots.The problem is that today the code generated by script setup creates TS compilation errors.
The following template creates an error
Binding element 'ok' implicitly has an 'any' type
.(You have the same error even if you don't destructure the slot value.)
This is quite a blocker because there's no good workaround:
ts-ignore
comments in the template.--noImplicitAny
but that's a strict flag that we really don't want to globally disable in our codebase.Of course, on top of the TS error, having typed slots would be a nice improvement for Volar.
What does the proposed API look like?
As a quick-fix / workaround, I suggest that the compiler adds
// @ts-ignore: implicit any
before the generated slot code.A better, long term fix would be to allow TS users to type their slots, I suggest:
The text was updated successfully, but these errors were encountered: