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

chore(project): update TypeScript to v4.8.3 #2360

Merged
merged 9 commits into from Oct 12, 2022

Conversation

joshblack
Copy link
Member

This PR updates TypeScript to v4.8.3. Generally, the changes made were around calls to React.cloneElement when the child arg was unknown. This lead to errors in the shape:

Argument of type '...' is not assignable to parameter of type 'Partial<unknown> & Attributes'.

This comes up in React.Children.{map,toArray} calls if the children type is the more general type React.ReactNode instead of a specific type that cloneElement is expecting.

For example, consider a List component that expects ListItem children:

interface Props {
  children: React.ReactNode
}

function List({ children }: Props) {
  return (
    <ul>
      {React.Children.map(children, (child, index) => {
        return React.cloneElement(child, { index });
      })}
    </ul>
  );
}

interface ListItemProps {
  children: string
  index: number
}

function ListItem({ children, index }: ListItemProps) {
  // do something with index
  return <li>{children}</li>
}

This example would encounter the above error since the author is expecting the child to be of type ListItemProps.

To address this, I believe we need to narrow the type through type assertions and this is the approach the PR takes

function List({ children }: Props) {
  return (
    <ul>
      {React.Children.map(children, (child, index) => {
-        return React.cloneElement(child, { index });
+        return React.cloneElement(child as React.ReactElement<ListItemProps>, { index });
      })}
    </ul>
  );
}

@joshblack joshblack requested review from a team and rezrah September 21, 2022 20:42
@changeset-bot
Copy link

changeset-bot bot commented Sep 21, 2022

🦋 Changeset detected

Latest commit: 6f8b95d

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
Copy link
Contributor

github-actions bot commented Sep 21, 2022

size-limit report 📦

Path Size
dist/browser.esm.js 77.66 KB (+0.01% 🔺)
dist/browser.umd.js 78.32 KB (+0.02% 🔺)

@joshblack joshblack temporarily deployed to github-pages September 21, 2022 20:49 Inactive
@joshblack joshblack temporarily deployed to github-pages September 21, 2022 21:19 Inactive
@joshblack joshblack temporarily deployed to github-pages October 7, 2022 15:28 Inactive
Copy link
Contributor

@colebemis colebemis left a comment

Choose a reason for hiding this comment

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

This seems reasonable 👍 Thanks for updating this, @joshblack!

Could these changes lead to runtime errors if these components are passed children that they don't expect? For example, would React.cloneElement(child as React.ReactElement<AvatarProps>, {size: 40}) cause problems if child is not React.ReactElement<AvatarProps>?

@joshblack joshblack temporarily deployed to github-pages October 10, 2022 16:23 Inactive
@joshblack joshblack temporarily deployed to github-pages October 10, 2022 18:14 Inactive
@joshblack joshblack temporarily deployed to github-pages October 12, 2022 19:40 Inactive
@joshblack joshblack merged commit 0f41dfe into main Oct 12, 2022
@joshblack joshblack deleted the chore/update-typescript-4.8.3 branch October 12, 2022 19:55
@primer-css primer-css mentioned this pull request Oct 12, 2022
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

2 participants