Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip nulls from the form state type for maximum polaris compatability #699

Conversation

airhorns
Copy link
Contributor

@airhorns airhorns commented Nov 22, 2024

Shopify Polaris, in its infinite wisdom, accepts only string | undefined for the value prop on its <TextInput/> component. <input/> in the DOM accepts string | readonly string[] | number | undefined;, which you'd think Polaris would be ok with, but alas no. At runtime, it's absolutely fine to pass null to the TextInput, it works fine, it's just the type that is not ok with it. Because Gadget's API responds with null, not undefined for fields that haven't been set yet, it means that the form values type have string | null | undefined in them, which causes a type error by default when wiring up to a Polaris <TextInput/> with a <Controller/>.

Because so many of our users use Polaris, we think we should adjust our default types to work correctly with Polaris inputs. Technically speaking, this type lies -- it suppresses nulls in the output types in favour of undefineds. That said, Gadget always quacks undefined and null for these fields, so the fact that it is optional is still captured in this output type, but the exact type is not captured correctly. I think that since we can't really change Polaris, this is still the right decision so that the base case works nicely for people.

I'm super open to other ideas but I ain't got any!

@airhorns airhorns marked this pull request as ready for review November 22, 2024 20:26
Shopify Polaris, in its infinite wisdom, accepts only `string | undefined` for the `value` prop on its `<TextInput/>` component. `<input/>` in the DOM accepts `string | readonly string[] | number | undefined;`, which you'd think Polaris would be ok with, but alas no. At runtime, it's absolutely fine to pass `null` to the TextInput, it works fine, it's just the type that is not ok with it. Because Gadget's API responds with `null`, not `undefined` for fields that haven't been set yet, it means that the form values type have `string | null | undefined` in them, which causes a type error by default when wiring up to a Polaris `<TextInput/>` with a `<Controller/>`.

Because so many of our users use Polaris, we think we should adjust our default types to work correctly with Polaris inputs. Technically speaking, this type lies -- it suppresses nulls in the output types in favour of undefineds. That said, Gadget always quacks undefined and null for these fields, so the fact that it is optional is still captured in this output type, but the exact type is not captured correctly. I think that since we can't really change Polaris, this is still the right decision so that the base case works nicely for people.
@airhorns airhorns force-pushed the harry/ggt-7368-inconsistent-typing-between-useactionform-and-api-client-and branch from 65a15fb to 73a84bb Compare November 22, 2024 20:40
Copy link
Contributor

@infiton infiton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:(

although the fact that there is a type only solution is nice

@airhorns airhorns merged commit 2f7f7c5 into main Nov 23, 2024
9 checks passed
@airhorns airhorns deleted the harry/ggt-7368-inconsistent-typing-between-useactionform-and-api-client-and branch November 23, 2024 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants