Skip to content

Commit c4191d7

Browse files
committed
lay ground work for better feature type inference
1 parent d77172b commit c4191d7

File tree

48 files changed

+625
-678
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+625
-678
lines changed

examples/react/custom-features/src/main.tsx

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ import type {
2121
Column,
2222
ColumnDef,
2323
OnChangeFn,
24-
RowData,
2524
Table,
2625
TableFeature,
27-
TableFeatures,
28-
TableState,
2926
Updater,
3027
} from '@tanstack/react-table'
3128
import type { Person } from './makeData'
@@ -50,66 +47,34 @@ export interface Table_Density {
5047
toggleDensity: (value?: DensityState) => void
5148
}
5249

53-
// Use declaration merging to add our new feature APIs and state types to TanStack Table's existing types.
54-
declare module '@tanstack/react-table' {
55-
// declare our new feature as a plugin
56-
interface Plugins {
57-
densityPlugin: TableFeature
58-
}
59-
// merge our new feature's state with the existing table state
60-
interface TableState_Plugins extends TableState_Density {}
61-
// merge our new feature's options with the existing table options
62-
interface TableOptions_Plugins extends TableOptions_Density {}
63-
// merge our new feature's instance APIs with the existing table instance APIs
64-
interface Table_Plugins extends Table_Density {}
65-
// if you need to add cell instance APIs...
66-
// interface Cell_Plugins extends Cell_Density {}
67-
// if you need to add row instance APIs...
68-
// interface Row_Plugins extends Row_Density {}
69-
// if you need to add column instance APIs...
70-
// interface Column_Plugins extends Column_Density {}
71-
// if you need to add header instance APIs...
72-
// interface Header_Plugins extends Header_Density {}
73-
74-
// Note: declaration merging on `ColumnDef` is not possible because it is a type, not an interface.
75-
// But you can still use declaration merging on `ColumnDef.meta`
76-
}
77-
78-
// end of TS setup!
79-
8050
// Here is all of the actual javascript code for our new feature
81-
export const densityPlugin: TableFeature = {
51+
export const densityPlugin: TableFeature<{
52+
Table: Table_Density
53+
TableOptions: TableOptions_Density
54+
TableState: TableState_Density
55+
}> = {
8256
// define the new feature's initial state
83-
getInitialState: <TFeatures extends TableFeatures>(
84-
initialState: Partial<TableState<TFeatures>>,
85-
): Partial<TableState<TFeatures>> => {
57+
getInitialState: (initialState) => {
8658
return {
8759
density: 'md',
8860
...initialState, // must come last
8961
}
9062
},
9163

9264
// define the new feature's default options
93-
getDefaultTableOptions: <
94-
TFeatures extends TableFeatures,
95-
TData extends RowData,
96-
>(
97-
table: Table<TFeatures, TData>,
98-
): TableOptions_Density => {
65+
getDefaultTableOptions: (table) => {
9966
return {
10067
enableDensity: true,
10168
onDensityChange: makeStateUpdater('density', table),
102-
} as TableOptions_Density
69+
}
10370
},
10471
// if you need to add a default column definition...
10572
// getDefaultColumnDef: <TFeatures extends TableFeatures, TData extends RowData>(): Partial<ColumnDef<TFeatures, TData>> => {
10673
// return { meta: {} } //use meta instead of directly adding to the columnDef to avoid typescript stuff that's hard to workaround
10774
// },
10875

10976
// define the new feature's table instance methods
110-
constructTableAPIs: <TFeatures extends TableFeatures, TData extends RowData>(
111-
table: Table<TFeatures, TData>,
112-
): void => {
77+
constructTableAPIs: (table) => {
11378
table.setDensity = (updater) => {
11479
const safeUpdater: Updater<DensityState> = (old) => {
11580
const newState = functionalUpdate(updater, old)

examples/svelte/basic-table-helper/src/App.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
const tableHelper = createTableHelper({
4747
_features: { columnSizingFeature: {} },
4848
_rowModels: {}, // client-side row models. `Core` row model is now included by default, but you can still override it here
49-
5049
debugTable: true,
5150
// TData: {} as Person, // optionally, set the TData type for the table helper. Omit if this will be a table helper for multiple tables of all different data types
5251
})

examples/svelte/basic-table-helper/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @ts-ignore
1+
// @ts-ignore - Svelte mount types not properly recognized
22
import { mount } from 'svelte'
33
import App from './App.svelte'
44

examples/svelte/column-ordering/src/App.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
columnVisibilityFeature,
2222
})
2323
24-
const columns: ColumnDef<any, Person>[] = [
24+
const columns: ColumnDef<typeof _features, Person>[] = [
2525
{
2626
header: 'Name',
2727
footer: (props) => props.column.id,

examples/svelte/filtering/src/App.svelte

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
return itemRank.passed
4040
}
4141
42-
const columns: ColumnDef<any, Person>[] = [
42+
const columns: ColumnDef<typeof _features, Person>[] = [
4343
{
4444
accessorFn: (row) => `${row.firstName} ${row.lastName}`,
4545
id: 'fullName',
@@ -75,7 +75,6 @@
7575
},
7676
onGlobalFilterChange: setGlobalFilter,
7777
globalFilterFn: fuzzyFilter,
78-
enableMultiRowSelection: true,
7978
})
8079
8180
$effect(() => {

examples/svelte/sorting/src/App.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
createSortedRowModel,
66
createTable,
77
createTableState,
8+
rowSortingFeature,
89
renderComponent,
910
sortFns,
11+
tableFeatures,
1012
} from '@tanstack/svelte-table'
1113
import Header from './Header.svelte'
1214
import './index.css'

examples/svelte/sorting/src/Header.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import type { Header, rowSortingFeature } from '@tanstack/svelte-table'
2+
import type { Header } from '@tanstack/svelte-table'
33
import type { Person } from './makeData'
44
import type { _features } from './tableHelper.svelte'
55

examples/vue/basic/src/App.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
createCoreRowModel,
55
useTable,
66
createColumnHelper,
7+
tableFeatures,
78
} from '@tanstack/vue-table'
89
import { ref } from 'vue'
910
@@ -43,7 +44,9 @@ const defaultData: Person[] = [
4344
},
4445
]
4546
46-
const columnHelper = createColumnHelper<any, Person>()
47+
const _features = tableFeatures({})
48+
49+
const columnHelper = createColumnHelper<typeof _features, Person>()
4750
4851
const columns = [
4952
columnHelper.group({

examples/vue/column-ordering/src/App.vue

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,66 @@ import {
77
type Column,
88
createColumnHelper,
99
type ColumnVisibilityState,
10+
columnOrderingFeature,
11+
tableFeatures,
1012
} from '@tanstack/vue-table'
1113
import { makeData, type Person } from './makeData'
1214
import { ref } from 'vue'
1315
import { faker } from '@faker-js/faker'
1416
15-
const columnHelper = createColumnHelper<any, Person>()
17+
const _features = tableFeatures({ columnOrderingFeature })
18+
19+
const columnHelper = createColumnHelper<typeof _features, Person>()
1620
1721
const data = ref(makeData(20))
18-
const columns = ref([
19-
columnHelper.group({
20-
header: 'Name',
21-
footer: (props) => props.column.id,
22-
columns: [
23-
columnHelper.accessor('firstName', {
24-
cell: (info) => info.getValue(),
25-
footer: (props) => props.column.id,
26-
}),
27-
columnHelper.accessor((row) => row.lastName, {
28-
id: 'lastName',
29-
cell: (info) => info.getValue(),
30-
header: () => 'Last Name',
31-
footer: (props) => props.column.id,
32-
}),
33-
],
34-
}),
35-
columnHelper.group({
36-
header: 'Info',
37-
footer: (props) => props.column.id,
38-
columns: [
39-
columnHelper.accessor('age', {
40-
header: () => 'Age',
41-
footer: (props) => props.column.id,
42-
}),
43-
columnHelper.group({
44-
header: 'More Info',
45-
columns: [
46-
columnHelper.accessor('visits', {
47-
header: () => 'Visits',
48-
footer: (props) => props.column.id,
49-
}),
50-
columnHelper.accessor('status', {
51-
header: 'Status',
52-
footer: (props) => props.column.id,
53-
}),
54-
columnHelper.accessor('progress', {
55-
header: 'Profile Progress',
56-
footer: (props) => props.column.id,
57-
}),
58-
],
59-
}),
60-
],
61-
}),
62-
])
22+
23+
const columns = ref(
24+
columnHelper.columns([
25+
columnHelper.group({
26+
header: 'Name',
27+
footer: (props) => props.column.id,
28+
columns: [
29+
columnHelper.accessor('firstName', {
30+
cell: (info) => info.getValue(),
31+
footer: (props) => props.column.id,
32+
}),
33+
columnHelper.accessor((row) => row.lastName, {
34+
id: 'lastName',
35+
cell: (info) => info.getValue(),
36+
header: () => 'Last Name',
37+
footer: (props) => props.column.id,
38+
}),
39+
],
40+
}),
41+
columnHelper.group({
42+
header: 'Info',
43+
footer: (props) => props.column.id,
44+
columns: [
45+
columnHelper.accessor('age', {
46+
header: () => 'Age',
47+
footer: (props) => props.column.id,
48+
}),
49+
columnHelper.group({
50+
header: 'More Info',
51+
columns: [
52+
columnHelper.accessor('visits', {
53+
header: () => 'Visits',
54+
footer: (props) => props.column.id,
55+
}),
56+
columnHelper.accessor('status', {
57+
header: 'Status',
58+
footer: (props) => props.column.id,
59+
}),
60+
columnHelper.accessor('progress', {
61+
header: 'Profile Progress',
62+
footer: (props) => props.column.id,
63+
}),
64+
],
65+
}),
66+
],
67+
}),
68+
]),
69+
)
6370
6471
const columnVisibility = ref<ColumnVisibilityState>({})
6572
const columnOrder = ref<ColumnOrderState>([])

examples/vue/column-pinning/src/App.vue

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import {
44
FlexRender,
55
createCoreRowModel,
66
useTable,
7+
columnPinningFeature,
8+
tableFeatures,
9+
columnVisibilityFeature,
710
} from '@tanstack/vue-table'
811
import type {
912
Column,
@@ -16,7 +19,12 @@ import { faker } from '@faker-js/faker'
1619
1720
const data = ref(makeData(5000))
1821
19-
const columnHelper = createColumnHelper<any, Person>()
22+
const _features = tableFeatures({
23+
columnPinningFeature,
24+
columnVisibilityFeature,
25+
})
26+
27+
const columnHelper = createColumnHelper<typeof _features, Person>()
2028
2129
const columns = ref([
2230
columnHelper.group({
@@ -116,7 +124,7 @@ const randomizeColumns = () => {
116124
)
117125
}
118126
119-
function toggleColumnVisibility(column: Column<any, any>) {
127+
function toggleColumnVisibility(column: Column<typeof _features, Person>) {
120128
columnVisibility.value = {
121129
...columnVisibility.value,
122130
[column.id]: !column.getIsVisible(),

0 commit comments

Comments
 (0)