Disable slots using defineSlots
#14461
Replies: 2 comments 2 replies
-
|
@rtcpw currently maintaining this approach? However, defineSlots<{}>() // weak
defineSlots<Record<string, never>>() // stronger intent |
Beta Was this translation helpful? Give feedback.
-
|
I do not think
defineSlots<{
default(props: { item: string }): any;
}>();Using an empty object: defineSlots<{}>();means “this component declares no typed slots”, but Vue template type checking generally does not treat arbitrary children as a hard error just because no slots are declared. At runtime, Vue will still accept children and expose them on So this is not something I would expect to be caught consistently: <ComponentA>
<div>Hello World</div>
</ComponentA>If you want a runtime development warning, you can check <script setup lang="ts">
import { useSlots, onMounted } from "vue";
const slots = useSlots();
onMounted(() => {
if (import.meta.env.DEV && slots.default) {
console.warn("ComponentA does not accept slot content.");
}
});
</script>Or, without waiting for mount: const slots = useSlots();
if (import.meta.env.DEV && slots.default) {
console.warn("ComponentA does not accept slot content.");
}That will not be a TypeScript error, but it gives consumers quick feedback during development. For library authoring, I would document it as a renderless component with no slot API, and optionally add the dev warning. Today, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a renderless component that does not use slots. I would like to specify that there are no slots using
defineSlots, so that TypeScript users can catch the error early if they specify a slot content. Is there a way to do this? This is what I want:In ComponentA:
In ComponentB:
Beta Was this translation helpful? Give feedback.
All reactions