Incorporating Vue 3 along with the composition API and TypeScript, I am facing an issue setting default values for props defined through an interface. When attempting to set a default value of []
for one of the props, I encounter a TypeScript error. How can I successfully set a default empty array?
<script setup lang="ts">
interface IProps {
title: string;
things: any[];
productUrl: any;
}
const props = withDefaults(defineProps<IProps>(), {
title: "",
things: [], //<-- error (see below)
productUrl: "",
});
The error message reads:
Type 'never[]' is not assignable to type '(props: Readonly<IProps>) => any[]'.
Additionally, it states:
The expected type comes from property 'things' which is declared here on type
'InferDefaults<Readonly<IProps>>'