The DataTableHeaders type is currently not exported (as of Vuetify 3.5.5). My guess is that this will change in the future, but in the meantime, you can still extract it from the VDataTable
.
Vuetify 3.5 (current)
Since v3.3, headers are deep readonly. We can undo the readonly, but not without changing the type, which makes it incompatible with the initial DataTableHeader declaration and requires an additional type assertion when assigning to the v-data-table.
So if you know you won't change the headers after declaration, I would just use the readonly declaration:
import type { VDataTable } from 'vuetify/components'
type ReadonlyHeaders = VDataTable['$props']['headers']
const headers: ReadonlyHeaders = [...]
This will give you an array type (DataTableHeader[]
). If you want the item type, you can further unwrap it:
import type { VDataTable } from 'vuetify/components'
type ReadonlyHeaders = VDataTable['$props']['headers']
type UnwrapReadonlyArray<A> = A extends Readonly<Array<infer I>> ? I : never;
type ReadonlyDataTableHeader = UnwrapReadonlyArrayType<ReadonlyHeaders>;
const headers: ReadonlyDataTableHeader[] = [...]
(the UnwrapReadonlyArray
utility type unwraps the readonly array and throws away the possible undefined
)
However, this turned out to need updating between Vuetify versions, while the array version did not. If possible, stick to the array version to avoid maintenance.
If you need to change the headers after declaration, you can make them mutable with another utility class:
type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }
type DataTableHeader = DeepMutable<ReadonlyDataTableHeader>;
(the DeepMutable
utility class maps the input type recursively to a new type, removing any readonly
modifiers)
Now if you assign a DataTableHeader[]
to the :headers
prop TS will complain that a DeepMutable[]
can't be assigned to a DeepReadonly[]
, but since we already know the types match, we can just tell it to shut up:
<v-data-table
:headers="(headers as any)"
Vuetify 3.3
Up until v3.3, VDataTable was still in labs, so the component had to be imported from vuetify/labs/components
. Also, Vuetify did not export a type, but only the constructor function:
import type { VDataTable } from 'vuetify/labs/components'
type Headers = InstanceType<typeof VDataTable>['headers']
(VDataTable
is a constructor function, so we turn it into a type with typeof
, get the type it constructs with InstanceType
and then get the headers
prop by index)
The array type did not include undefined
, but could be an array of arrays. So unwrapping the item type was slightly different:
type UnwrapReadonlyArrayType<A> = A extends Readonly<Array<infer I>> ? UnwrapReadonlyArrayType<I> : A
type ReadonlyDataTableHeader = UnwrapReadonlyArrayType<Headers>;
(the UnwrapArrayType
recursively extracts the item type from an array type until no array is left and we have the DataTableHeader
)
As with 3.4, this gives you readonly types, which can be unwrapped as above.
Until Vuetify 3.3
Headers were not readonly, otherwise it is the same as above:
import { VDataTable } from 'vuetify/labs/components'
type Headers = InstanceType<typeof VDataTable>['headers'] // gives `DataTableHeader[] | DataTableHeader[][]`
// optionally unwrap DataTableHeader
type UnwrapArrayType<A> = A extends Array<infer I> ? UnwrapArrayType<I> : A
type DataTableHeader = UnwrapArrayType<Headers>