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

Avoid tokens weird type checking/eliding #2762

Merged
merged 10 commits into from
Jan 11, 2023
Merged

Conversation

mattcosta7
Copy link
Collaborator

Describe your changes here.

Relates to #2760

In this pr we omitted onResize and onResizeCapture as typescript was causing these to elide a mandator unknown type, which was super confusing.

I believe this was caused by the fact that these props aren't equally defined across all polymorphic types, and forwardRef swallows polymorphism, making things wonky.

I've re-typed these components using the polymorphic helper, and added some type tests to help validate they don't regress. I think we would be well served extending our type tests further to cover most/all components, as I'm sure others are limited in the same or similar ways

Screenshots

Please provide before/after screenshots for any visual changes

Merge checklist

  • Added/updated tests
  • Added/updated documentation
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge

Take a look at the What we look for in reviews section of the contributing guidelines for more information on how we review PRs.

@mattcosta7 mattcosta7 self-assigned this Jan 11, 2023
@mattcosta7 mattcosta7 requested a review from a team January 11, 2023 04:02
@changeset-bot
Copy link

changeset-bot bot commented Jan 11, 2023

🦋 Changeset detected

Latest commit: 3ea4e32

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot temporarily deployed to storybook-preview-2762 January 11, 2023 04:09 Inactive
@github-actions
Copy link
Contributor

github-actions bot commented Jan 11, 2023

size-limit report 📦

Path Size
dist/browser.esm.js 87.38 KB (0%)
dist/browser.umd.js 88.03 KB (0%)

@github-actions github-actions bot temporarily deployed to storybook-preview-2762 January 11, 2023 04:14 Inactive
@mattcosta7 mattcosta7 temporarily deployed to github-pages January 11, 2023 04:17 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-2762 January 11, 2023 04:17 Inactive
@mattcosta7 mattcosta7 temporarily deployed to github-pages January 11, 2023 04:33 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-2762 January 11, 2023 04:34 Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-2762 January 11, 2023 04:36 Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-2762 January 11, 2023 04:42 Inactive
@mattcosta7 mattcosta7 temporarily deployed to github-pages January 11, 2023 04:43 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-2762 January 11, 2023 04:43 Inactive
@@ -1,14 +1,15 @@
import React, {forwardRef, MouseEventHandler} from 'react'
import Box from '../Box'
import {merge, SxProp} from '../sx'
import {BetterSystemStyleObject, merge, SxProp} from '../sx'
Copy link
Member

Choose a reason for hiding this comment

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

👍🏼

Copy link
Member

@broccolinisoup broccolinisoup left a comment

Choose a reason for hiding this comment

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

I believe this was caused by the fact that these props aren't equally defined across all polymorphic types, and forwardRef swallows polymorphism, making things wonky.

This makes perfect sense!

I think we would be well served extending our type tests further to cover most/all components, as I'm sure others are limited in the same or similar ways.

Thanks for the suggestion ✨ I see there are type tests for quite a few components but the recent ones seem to lack of them. I'll chat with the team about it.

This looks great to me! 🚀 Thanks for raising this PR 🙏🏼

@joshblack
Copy link
Member

I believe this was caused by the fact that these props aren't equally defined across all polymorphic types, and forwardRef swallows polymorphism, making things wonky.

Would there be a good place to learn more about how forwardRef swallows polymorphism? (Or this concept more broadly). It was the first time I had seen the output of a type get inlined in such a way that information was lost and I would love to learn more about it if you have any suggestions 👀

For the type tests, is this something that should be a part of the testing requirements for components/props added or changed in a component? It doesn't really seem like there are other alternatives to guarantee that things are showing up, curious if there are other approaches that teams use or if this is kind of our best best moving forward to catch issues like this. Would love to hear your thoughts!

@mattcosta7
Copy link
Collaborator Author

mattcosta7 commented Jan 11, 2023

I believe this was caused by the fact that these props aren't equally defined across all polymorphic types, and forwardRef swallows polymorphism, making things wonky.

Would there be a good place to learn more about how forwardRef swallows polymorphism? (Or this concept more broadly). It was the first time I had seen the output of a type get inlined in such a way that information was lost and I would love to learn more about it if you have any suggestions 👀

I don't know of one offhand, but a quick search brings up things like

https://fettblog.eu/typescript-react-generic-forward-refs/
https://twitter.com/jaredpalmer/status/1217149616082296833?lang=en

the crux of the issue is that a HOC in a static context can't return a generic that infers from an internal generic which seem related. Which is a mouthfull but this might be more easy to grok

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/0e64955901599be1e43a2e50c556d141a2b8fdd8/types/react/index.d.ts#L776-L781

the types for forwardRef don't return a component that's possible of generically defining something based on a certain prop, so it effectively 'swallows' up the as prop (which can't be inferred because it can't be defined)

For the type tests, is this something that should be a part of the testing requirements for components/props added or changed in a component? It doesn't really seem like there are other alternatives to guarantee that things are showing up, curious if there are other approaches that teams use or if this is kind of our best best moving forward to catch issues like this. Would love to hear your thoughts!

I think there definitely should be some level of requirement for adding these, even if they're only limited in the scope of the props validated!

@mattcosta7
Copy link
Collaborator Author

Not sure offhand whether this would be something we want to land in the current/next release - so feel free to merge when it makes sense (I don't want to merge too early!)

@joshblack
Copy link
Member

@mattcosta7 makes perfect sense, thanks so much!

@broccolinisoup I'm going to merge this is as the follow-up to the TODO from: #2760 if that makes sense, let me know if it makes the release more complicated and I'd be happy to revert/add it back in after the release 👍

@joshblack joshblack merged commit 74016a7 into main Jan 11, 2023
@joshblack joshblack deleted the update-types-for-tokens branch January 11, 2023 17:17
@primer-css primer-css mentioned this pull request Jan 11, 2023
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.

None yet

3 participants