Skip to content

Version 2: looping over slots as a variable causes error TS2345 #4025

Closed
@isazulay

Description

@isazulay

Use case:
Having a wrapper component, I want the parent to be able to populate the slots of the wrapped component.

Implementation:

<template>
  <v-alert
    v-bind="props"
    variant="outlined">
    <template v-for="(_, slot) in slots" #[slot]="scope">
      <slot :name="slot" v-bind="scope || {}" />
    </template>
  </v-alert>
</template>

<script setup lang="ts">
import type { VAlert } from 'vuetify/components/VAlert';
import type { Props } from './types/CustomAlert.model';

const props = defineProps<Props>();
const slots = defineSlots<VAlert['$slots']>();
</script>

Current behavior:
The use case is still possible to run with the same code prior to v2 of vue-tsc, but running the vue-tsc command return the following error:

error TS2345: Argument of type '{ props?: Record<string, any> | undefined; }' is not assignable to parameter of type '{ props: Record<string, any>; }'.
  Types of property 'props' are incompatible.
    Type 'Record<string, any> | undefined' is not assignable to type 'Record<string, any>'.
      Type 'undefined' is not assignable to type 'Record<string, any>'.

Expected behavior:
Running vue-tsc should not throw an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    design limitationgood reproduction ✨This issue provides a good reproduction, we will be able to investigate it first

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions