How to initialize/give default value to array in DefineProps when I pass array as prop in vue 3 script setup
P粉155551728
P粉155551728 2024-03-25 18:57:15
0
1
486

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粉155551728
P粉155551728

reply all(1)
P粉436688931

document

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

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

copy

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template