Description
TanStack Table version
v8.20.6
Framework/Library version
Core
Describe the bug and the steps to reproduce it
The CoreOptions#state is set to Partial<TableState>
allowing consumers to omit any property on the state. However, this also means that consumers can set properties to undefined
. This is not accounted for when merging in new options.state
. When a state property is passed in as undefined
, it will still overwrite whatever value was there before.
The type of table.options.state
and return of table.getState()
(TableState
) has many (all?) of its properties as required, so when the above occurs, undefined can be set for a required property. This type violation is being hidden by the type assertion in setOptions
As a result, various places throughout the code base that trust the TableState
type can end up try to access fields on undefined values, and causing a crash. Such as #4697
Some places account for this by checking for undefined
(even though the type says it is not possible), however there are still cases where this is not considered, such as:
grouping
can be undefined when accessinggrouping.length
in getGroupedRowModel.- deconstructing
pagination
in getPaginationRowModel - and likely many more...
Either:
The type for table.options.state
and return of table.getState()
should be changed to Partial<TableState>
OR
The setOptions should account for undefined
values by utilizing something like Object.keys(newState).forEach((key) => { if (key in newState && newState[key] === undefined) delete newOptions.state[key]})
prior to merging it with the current state.
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)
N/A
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.
Activity