Home  >  Q&A  >  body text

How to initialize/give default value to array in DefineProps when I pass array as prop in vue 3 script setup

I have passed an array as prop item and I gave it a type in the interface Props and when I try to give it a default value I get the error line 4 TS2322: Type 'never[ ]'Not assignable to type'(props: readonly<Props>) = >String[]'. Type 'never[]' provides no match for signature '(props: Readonly<Props>): string[]'. I'm not sure what I'm doing wrong here since this seems to apply to other variables

<script setup lang="ts">
import {ref} from "vue";

interface Props {
  items?: Array<string>
}

const props = withDefaults(defineProps<Props>(), {
  items: []
});
let selectedItem = ref(props.items[0])

P粉155551728P粉155551728207 days ago417

reply all(1)I'll reply

  • P粉436688931

    P粉4366889312024-03-26 17:16:21

    document

    This applies to both the Options API and the Combination API!

    const props = withDefaults(defineProps(), {
      items: () => []
    });
    

    copy

    reply
    0
  • Cancelreply