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:
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
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 toundefined. This is not accounted for when merging in newoptions.state. When a state property is passed in asundefined, it will still overwrite whatever value was there before.The type of
table.options.stateand return oftable.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 setOptionsAs a result, various places throughout the code base that trust the
TableStatetype can end up try to access fields on undefined values, and causing a crash. Such as #4697Some 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:groupingcan be undefined when accessinggrouping.lengthin getGroupedRowModel.paginationin getPaginationRowModelEither:
The type for
table.options.stateand return oftable.getState()should be changed toPartial<TableState>OR
The setOptions should account for
undefinedvalues by utilizing something likeObject.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