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

Add support for React 19 #879

Merged
merged 1 commit into from
Jan 2, 2025
Merged

Add support for React 19 #879

merged 1 commit into from
Jan 2, 2025

Conversation

remcohaszing
Copy link
Member

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and discussions and couldn’t find anything or linked relevant results below
  • I made sure the docs are up to date
  • I included tests (or that’s not needed)

Description of changes

This adds support for React 19 types, by removing the dependency on the types provided by hast-util-to-jsx-runtime, thus removing the dependency on the JSX global namespace.

Unfortunately the current setup doesn’t really allow testing the types against multiple React versions. #838 would enable that.

Closes #877

@remcohaszing remcohaszing added 🐛 type/bug This is a problem ☂️ area/types This affects typings 👶 semver/patch This is a backwards-compatible fix 🤞 phase/open Post is being triaged manually labels Dec 20, 2024

Choose a reason for hiding this comment

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

index.js:4

  • [nitpick] The re-addition of the ExtraProps typedef seems redundant and could be confusing. Consider removing it if not necessary.
* @typedef {import('./lib/index.js').ExtraProps} ExtraProps
Copy link
Member

@ChristianMurphy ChristianMurphy left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @remcohaszing

Comment on lines +236 to +238
// @ts-expect-error
// React components are allowed to return numbers,
// but not according to the types in hast-util-to-jsx-runtime
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 Author

Choose a reason for hiding this comment

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

Somewhat yes. But also apparently hast-util-to-jsx-runtime doesn’t support JSX.ElementType, which JSX runtimes may optionally define. That adds another complexity on top of that issue.

Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to solve such things instead of ts-expect-error

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree, but that issue has been proven hard to resolve upstream. (If it can be resolved upstream at all.) This @ts-expect-error is internal anyway, and this change unblocks a lot of users.

@bluepeter
Copy link

Let's make this happen!

@wooorm
Copy link
Member

wooorm commented Jan 1, 2025

Why does Copilot review things?

/**
* @typedef {{
* [Key in Extract<ElementType, string>]?: ElementType<ComponentProps<Key> & ExtraProps>
* }} Components
* Map tag names to components.
Copy link
Member

Choose a reason for hiding this comment

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

I believe this type is not equivalent to the previous Components.
But this is listed as a patch.
Is it intentional that you remove keyof JSX.IntrinsicElements?

Copy link
Member Author

Choose a reason for hiding this comment

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

ElementType includes JSX.IntrinsicElements.

Copy link
Member

@wooorm wooorm Jan 2, 2025

Choose a reason for hiding this comment

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

@remcohaszing No, I mean keyof? Does this allow b: 'i'. I assume not, because i or all other element names are not related to props?

Copy link
Member Author

Choose a reason for hiding this comment

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

It does! And there’s also a pre-existing test which covers this.

Copy link
Member

Choose a reason for hiding this comment

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

Weird, I do not see it in the source

Copy link
Member Author

Choose a reason for hiding this comment

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

renderToStaticMarkup(<Markdown children="# a" components={{h1: 'h2'}} />),

Copy link
Member

Choose a reason for hiding this comment

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

right no that, I meant in the @types/react code

Copy link
Member

Choose a reason for hiding this comment

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

It feels like it could be a bug that @types/react supports this today — that they could remove it somewhere in the future

Copy link
Member

Choose a reason for hiding this comment

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

not sure where it happens but when I change that line to h1: 'x-y' we get a type error — which indicates that I should worry less because it is type checked — even though the types should likely allow custom elements like that.

lib/index.js Outdated Show resolved Hide resolved
Comment on lines +236 to +238
// @ts-expect-error
// React components are allowed to return numbers,
// but not according to the types in hast-util-to-jsx-runtime
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to solve such things instead of ts-expect-error

@ChristianMurphy
Copy link
Member

Why does Copilot review things?

What a very somber and philosophical question 😆
Why do you review things?
Why does anyone do anything?


If you are asking why it appeared on this PR as a reviewer.
It is because I invited it as an additional reviewer to see how it would do.

This adds support for React 19 types, by removing the dependency on the
types provided by hast-util-to-jsx-runtime, thus removing the dependency
on the JSX global namespace.
@wooorm
Copy link
Member

wooorm commented Jan 2, 2025

If you are asking why it appeared on this PR as a reviewer.
It is because I invited it as an additional reviewer to see how it would do.

And, are you happy with it? 😅

I am emboldened in my view that I am not happy with it 😅

@remcohaszing
Copy link
Member Author

I’m fine with @ChristianMurphy wanting to try Copilot reviews, but IMO the result is really pointless, at least in this case.

@wooorm
Copy link
Member

wooorm commented Jan 2, 2025

Of course! Always up for trying things out!

@wooorm wooorm merged commit b151a90 into main Jan 2, 2025
4 checks passed
@wooorm wooorm deleted the react-19 branch January 2, 2025 14:59

This comment has been minimized.

@wooorm wooorm added the 💪 phase/solved Post is done label Jan 2, 2025
@github-actions github-actions bot removed the 🤞 phase/open Post is being triaged manually label Jan 2, 2025
@ChristianMurphy
Copy link
Member

And, are you happy with it? 😅

It's not perfect, but getting better.

I am emboldened in my view that I am not happy with it 😅

Okay

the result is really pointless, at least in this case.

It was in this case.
It is getting less noisy and nitpicky which is good.
I wish there were a way to specify custom criteria.

@cyberluke
Copy link

Why not reuse this solution: quantizor/markdown-to-jsx@918b44b#diff-4b76aeed93d86ab1e483e12333e1bae64ccdc1a1e4fd27bec2b32f41db08e6cfR5

and just change:
JSX.IntrinsicAttributes

to
React.JSX.IntrinsicAttributes

in complex-types.ts and complex-types.d.ts?

@remcohaszing
Copy link
Member Author

The goal of hast-util-to-jsx-runtime is to support any JSX runtime, not just React.

@wooorm
Copy link
Member

wooorm commented Jan 6, 2025

Released in 9.0.2!

@sanskar-19
Copy link

sanskar-19 commented Jan 6, 2025

Released in 9.0.2!

@wooorm - Ugh, the type declaration files seems to be missing?

@wooorm
Copy link
Member

wooorm commented Jan 6, 2025

done in 9.0.3!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
☂️ area/types This affects typings 💪 phase/solved Post is done 👶 semver/patch This is a backwards-compatible fix 🐛 type/bug This is a problem
Development

Successfully merging this pull request may close these issues.

React 19 support
6 participants