Table: accept readonly arrays in the `data` prop
Package
v4.x
Description
UTable declares its data prop as T[], so it rejects readonly arrays, although it never mutates the caller's array. Readonly arrays come from Vue's readonly() and from libraries that expose shared state as readonly, such as query caches.
<script setup lang="ts">
import type { TableColumn } from '@nuxt/ui'
type Invitation = {
id: string
email: string
}
const invitations = computed<readonly Invitation[]>(() => [
{ id: '1', email: '[email protected]' }
])
const columns: TableColumn<Invitation>[] = [
{ accessorKey: 'email', header: 'Email' }
]
</script>
<template>
<!-- Type error: 'readonly Invitation[]' is 'readonly' and cannot be assigned to the mutable type 'Invitation[]'. -->
<UTable :data="invitations" :columns="columns" />
</template>
Today, the consumer has to cast the array or pass a copy such as [...invitations] only to satisfy the prop type. For example, orpc-vue types query data as deeply readonly because it is shared cache state, so every table bound to a query needs such a workaround.
Proposed change
Declare the prop as data?: readonly T[] in src/runtime/components/Table.vue, and initialize the internal state from a copy, as the props.data watcher already does:
const data = createRef(props.data ? [...props.data] : [], props.watchOptions?.deep !== false)
Without the copy, the internal ref would be typed readonly T[], which TanStack Table rejects because it types data as TData[]. Mutable arrays keep working unchanged.
Additional context
Hi! I'd like to pick this up if you're not planning to send a PR yourself. My plan is to change UTable's data prop type from T[] to readonly T[] (and check the internal TanStack table options and the slot types so nothing needs a mutable array). I'd add a type test with a computed<readonly T[]> like the one in your repro. Could you assign it to me?