I'm facing a TypeScript error while trying to bind the value of apiRes to v-model. Below is the code snippet where the error occurred. How can I resolve this issue?
Arguments of type { modelValue: Cars | undefined; } cannot be assigned to type NonNullable<(Partial<{}> & Omit<{ readonly modelValue?: Car | undefined; } & VNodeProps & AllowedComponentProps & ComponentCustomProps & Readonly<...> , never> & Record <...>) | (Partial<...> & ... 1 more ... & Record<...>)> & Parameters of Record<...>.
<template>
<div>
<component :is="component" v-model="apiRes" />
</div>
</template>
<script lang="ts" setup>
import test1 from './test1.vue'
import test2 from './test2.vue'
import { computed, ref } from 'vue'
export interface Car {
age: number
name: string
}
export interface Car2 {
door: string
cvt: string
}
type Cars = Car | Car2
const component = computed(() => {
const random = Math.random()
return random > 0.5 ? test1 : test2
})
const apiRes = ref<Cars>()
</script>
//test1
<template>
<div>{{ props }}</div>
</template>
<script setup lang="ts">
import type { Car } from './test.vue'
const props = defineModel<Car>()
</script>
//test2
<template>
<div>{{ props }}</div>
</template>
<script setup lang="ts">
import type { Car2 } from './test.vue'
const props = defineModel<Car2>()
</script>
The error vanishes if I change the following line of code, but it might not be the ideal solution:
const apiRes = ref({} as Car)