petyosi / react-virtuoso

The most powerful virtual list component for React
https://virtuoso.dev
MIT License
5.25k stars 301 forks source link

[BUG] Incompatible with `exactOptionalPropertyTypes` option #893

Closed Karibash closed 1 year ago

Karibash commented 1 year ago

Describe the bug When the option exactOptionalPropertyTypes is enabled, the optional properties of Virtuoso components cannot be passed undefined.

The type must be changed to accept undefined as shown below. https://github.com/DefinitelyTyped/DefinitelyTyped/blob/12bdd6bfdd39be5e75a3f279705ba373a344ce63/types/react/index.d.ts#L1870

Reproduction https://codesandbox.io/p/sandbox/vigilant-andras-5eoozu?file=%2Fsrc%2FApp.tsx

To Reproduce Steps to reproduce the behavior:

  1. Enable the exactOptionalPropertyTypes option in tsconfig.json.
  2. Pass undefined to optional properties of Virtuoso components.

Expected behavior Allows passing undefined to optional properties of Virtuoso components.

Screenshots

スクリーンショット 2023-05-02 18 46 18
petyosi commented 1 year ago

What you describe as a bug is an intentional design decision - the optional properties don't accept undefined. Either set them to something meaningful or don't set them at all.

I do understand that it might be of a slight inconvenience for you, but passing undefined to a property value is an extremely complex to handle, given the stateful nature of the component. There are multiple possible ways to handle it, with forks depending on what the previous prop value was.

Karibash commented 1 year ago

I will share a workaround for those who are having similar problems. https://codesandbox.io/p/sandbox/sparkling-meadow-fb9cx8?file=%2Fsrc%2FApp.tsx

petyosi commented 1 year ago

To clarify, the workaround above is one way to handle undefined (as in, ignore it). However setting certain property to something and then setting it to undefined might be done with different expectations. For example, as a way to revert it to the "default" value. Or, maybe, to some other, special value.