Cast required to access custom options when defining a custom plugin/feature
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- react, typescript
- Domain
- developer-experience
Research direction
Start with packages/table-core/src/types/TableFeatures.ts and compare its published types with the custom-plugin example and custom features guide. Reproduce the TypeScript errors in the linked example, then determine how custom feature options should be exposed without casts or whether the documentation must be corrected. Done means the intended custom-plugin usage type-checks consistently and the guide matches the supported API.
Written by the indexing model from the issue text.
Description
TanStack Table version
9.2.4
Framework/Library version
Vue 3.5.43
Describe the bug and the steps to reproduce it
When defining a custom plugin/feature, a cast is required to access any options that it adds. For example, this is can be observed in an official example for custom plugins--on lines 108 and 119, table.options requires a cast to access onDensityChange. Here's the relevant code for convenience:
constructTableAPIs: (table) => {
assignTableAPIs('densityPlugin', table, {
table_setDensity: {
fn: (updater: Updater<DensityState>) => {
const safeUpdater: Updater<DensityState> = (old) => {
const newState = functionalUpdate(updater, old)
return newState
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
},
},
table_toggleDensity: {
fn: (value?: DensityState) => {
const safeUpdater: Updater<DensityState> = (old) => {
if (value) return value
return old === 'lg' ? 'md' : old === 'md' ? 'sm' : 'lg' // cycle through the 3 options
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
},
},
})
},
In theory, the internal Table_Internal type used in the TableFeature definition should be able to load options added by plugins, but this type is stripped from the published code (I guess using the code from this PR?), so the type exposed to custom plugin authors is the regular Table, which does not come with plugins by default. Interestingly, Table has support for Table<any, any> to load all plugins, but trying to set that on the table in the constructTableAPIs function causes its own type error since that doesn't match the type set by TableFeature.
export const densityPlugin: TableFeature = {
constructTableAPIs: (table: Table<any, any>) => {
// can now access table.options.onDensityChange with the correct types, but now this function itself causes a type error:
/*
Type
<TFeatures extends TableFeatures, TData extends RowData>(table: Table<any, any>) => void
is not assignable to type
<TFeatures extends TableFeatures, TData extends RowData>(table: Table<TFeatures, TData>) => void
*/
}
}
I'm not sure what the best way to solve this is; I see a few possibilities:
- leave it as is; a couple type casts isn't a huge deal.
- in this case, the docs should be updated--e.g., the custom features guide says "If the TypeScript types are set up correctly, you should have no TypeScript errors when you create the feature object with the new state, options, and instance APIs." but the example code immediately afterwards does not have the cast, so it actually does have TS errors.
- change TableFeature to use a different/new
Table_Alltype that actually gets published - change TableFeature to accept generic parameters for its own custom options/state/etc.? (not sure we'd have access to the right TFeatures/TData then, though, and I have no idea what that would do to typechecking performance.)
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)
https://tanstack.com/table/latest/docs/framework/react/examples/custom-plugin
Screenshots or Videos (Optional)
No response
Do you intend to try to help solve this bug with your own PR?
None
Terms & Code of Conduct
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
- Dominant language
- TypeScript
- Stars
- 28.4k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 7
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from TanStack/table
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
Difficulty 3/5 1-2 days Newbie friendliness 78/100
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug v2
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
modelcontextprotocol/inspector#2458 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 75/100
railmapgen/rmp-gallery#4068 ·
-
Mend: dependency security vulnerability status: needs triage 🕵️♀️
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
carbon-design-system/ibm-products#9907 ·