Working with vue3 + vite + ts, I customized a text editor using tiptap. However, when I try to access item.type
, item.icon
, or any other item attributes, they all show 'error'
.
This indicates that ToolbarItem | Divider
does not have certain attributes:
item.type => Property 'type' does not exist on type 'ToolbarItem | Divider', Property 'type' does not exist on type 'ToolbarItem'
,
In the parent component code:
<template v-for="(item, index) in items" :key="index">
<div v-if="item.type === 'divider'" class="divider" :key="`divider${index}`" />
<Child v-else :key="index"
:icon="item.icon" :title="item.title"
:action="item.action" :isActive="item.isActive"/>
</template>
interface ToolbarItem {
icon: string,
title: string,
action: Function,
isActive?: Function
}
interface Divider {
type: string
}
const items: Array<ToolbarItem | Divider> = [
{
icon: 'bold',
title: 'Bold',
action: () => props.editor.commands.toggleBold(),
isActive: () => props.editor.isActive('bold'),
},
{
type: 'divider'
},
...
]
In the Child component code:
interface ToolbarButtonProps {
icon: string,
title: string,
action: Function,
isActive?: Function
}
const props = defineProps<ToolbarButtonProps>()
I am looking for a way to inject items separately into the div or Child components to avoid the error messages.