Disable slots using defineSlots
#14461
Replies: 3 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.
-
|
Has this changed, or perhaps im misunderstanding something. If i try to use a named slot that is not in defined slots I will actually get a Also is it intentional that this works by only checking inclusion? I.e. if i try to define type slotsType =
| ({a : boolean} & {b: never))
| ({a : never} & {b: number))using both a and b slots will not raise an error. To give context on where the 2nd scenario can be useful: im trying to make a datatable where you can either override how certain cells are rendered, or get an escape hatch for the entire row, using both at the same time is not possible |
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