Skip to content

TableState does not account for options.state properties being set as undefined #5909

Open
@Beiufin

Description

@Beiufin

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

  • 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions