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

react: fix to classify @types/react as a peer dependency #2281

Merged
merged 4 commits into from Oct 19, 2023

Conversation

remcohaszing
Copy link
Member

Since react is a peerDependency, so should it’s matching type definitions be. However, in order not to bother people not using TypeScript, it’s made optional.

This solves dependency resolution for people using React lower than latest. A practical example:

Create an emtpy directory, and cd into it. Now run:

$ yarn add @mdx-js/react @types/react@17 react@17

And inspect the dependency tree:

$ yarn list                                      
yarn list v1.22.19
├─ @mdx-js/react@2.3.0
│  ├─ @types/mdx@^2.0.0
│  ├─ @types/react@>=16
│  └─ @types/react@18.0.31
│     ├─ @types/prop-types@*
│     ├─ @types/scheduler@*
│     └─ csstype@^3.0.2
├─ @types/mdx@2.0.4
├─ @types/prop-types@15.7.5
├─ @types/react@17.0.55
│  ├─ @types/prop-types@*
│  ├─ @types/scheduler@*
│  └─ csstype@^3.0.2
├─ @types/scheduler@0.16.3
├─ csstype@3.1.1
├─ js-tokens@4.0.0
├─ loose-envify@1.4.0
│  └─ js-tokens@^3.0.0 || ^4.0.0
├─ object-assign@4.1.1
└─ react@17.0.2
   ├─ loose-envify@^1.1.0
   └─ object-assign@^4.1.1

Alternatively:

$ find . -name package.json | sort
./node_modules/csstype/package.json
./node_modules/js-tokens/package.json
./node_modules/loose-envify/package.json
./node_modules/@mdx-js/react/node_modules/@types/react/package.json  # React 18 types
./node_modules/@mdx-js/react/package.json
./node_modules/object-assign/package.json
./node_modules/react/package.json                                    # React 17
./node_modules/@types/mdx/package.json
./node_modules/@types/prop-types/package.json
./node_modules/@types/react/package.json                             # React 17 types
./node_modules/@types/scheduler/package.json
./package.json

You can see there are two versions of @types/react. This may cause hard to debug conflicts.

This solves the problem people described in https://github.com/orgs/mdx-js/discussions/2238. This is not intentional. They are just lucky react doesn’t ship their own type definitions.

Since `react` is a peerDependency, so should it’s matching type
definitions be. However, in order not to bother people not using
TypeScript, it’s made optional.
@remcohaszing remcohaszing added 🐛 type/bug This is a problem 📦 area/deps This affects dependencies ☂️ area/types This affects typings 👶 semver/patch This is a backwards-compatible fix 🤞 phase/open Post is being triaged manually labels Mar 29, 2023
@remcohaszing remcohaszing requested a review from a team March 29, 2023 13:30
@vercel
Copy link

vercel bot commented Mar 29, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
mdx ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 19, 2023 1:05pm

@codecov-commenter
Copy link

codecov-commenter commented Mar 29, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (a34177c) 100.00% compared to head (1ddd6af) 100.00%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #2281   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           23        23           
  Lines         2499      2499           
  Branches         4         4           
=========================================
  Hits          2499      2499           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wooorm
Copy link
Member

wooorm commented Mar 29, 2023

I think this will break several setups: https://github.com/orgs/mdx-js/discussions/2238#discussioncomment-5455250.

@wooorm
Copy link
Member

wooorm commented Mar 29, 2023

If this is done, it should not be done to only react?

@remcohaszing
Copy link
Member Author

This would only break for users who have relied on @types/react being a transitive dependency instead of an explicit one. TypeScript will show them an error they need to install @types/react manually, which they should have done in the first place.

This should be done for all frameworks that don’t ship their own types. By coincidence this is only react. preact and vue ship their own type definitions.

@wooorm
Copy link
Member

wooorm commented Mar 29, 2023

You are completely right that that’s what should happen in npm. It is also theoretically better. But it depends on a a recent undocumented change in npm.

I am unsure about this change because we operate at a large scale with this very popular package.

There are several things that cause my hesitation:

This makes me quite cautious about such changes at a large scale.
I am fine with testing the waters in smaller projects first.
I am not blocking this, but not approving it either.

@remcohaszing
Copy link
Member Author

  1. Dependencies indicate that a dependency should be resolvable for a package, but it’s ok if there are other versions somewhere else in node_modules.
  2. Peer dependencies indicate there should be one deduplicated occurrence of a package.

Given a package b which has major versions 1 to 18.

  1. If package a has a dependency on b>=16, a package manager is free to install either version 16, 17, or 18. This dependency may be located either in node_modules/b, or node_modules/a/node_modules/b.
  2. If package a has a peer dependency on b>=16, a package manager is free to install either version 16, 17, or 18. This dependency must be located in node_modules/b. A package manager can either install this automatically, or delegate this responsibility to the user.

A libary for a framework should have a peer dependency on the framework. This the same for framework types. If a user uses react@16, they should also use @types/react@16.

Optional peer dependencies continues on the concept of peer dependencies. It means a b may be used if installed alongside a, but it’s not required for a to function. This applies to @types/react. @mdx-js/react can function fine without @types/react, but it’s required to make the TypeScript types work.

I’m pretty adamant @types/react belongs in peerDependencies alongside react. It’s already causing issues for Yarn users. I see this also matches what react-markdown does.

I also think @types/react should be optional, because otherwise it forces non-TypeScript users to install @types/react. IMO not ideal, but also it’s not that bad of an issue.

I can remove the peerDependenciesMeta if that’s what’s needed to convince you.

@wooorm
Copy link
Member

wooorm commented Oct 19, 2023

I also think @types/react should be optional, because otherwise it forces non-TypeScript users to install @types/react. IMO not ideal, but also it’s not that bad of an issue.

I don’t think whether someone uses TS or not can be inferred from optional? I feel like we discussed this elsewhere?
Given the package managers are different, I’d assume some will install it for users that don’t use TS, and that some won’t for users that do?

@remcohaszing
Copy link
Member Author

I don’t think whether someone uses TS or not can be inferred from optional?

No, it can’t. Unfortunately npm and other package managers don’t have the concept of optional dependencies based on context. An optional peer dependency basically means the package manager will show a warning or error if a non-matching version is installed alongside the package. It’s kind of similar to the engines field.

packages/react/package.json Outdated Show resolved Hide resolved
Signed-off-by: Titus <tituswormer@gmail.com>
@wooorm wooorm changed the title Make @types/react an optional peerDependency of @mdx-js/react react: fix to classify @types/react as a peerDependency Oct 19, 2023
@wooorm wooorm changed the title react: fix to classify @types/react as a peerDependency react: fix to classify @types/react as a peer dependency Oct 19, 2023
@wooorm wooorm merged commit 172e519 into main Oct 19, 2023
10 checks passed
@wooorm wooorm deleted the react-types-optional-peer-dependency branch October 19, 2023 13:12
@wooorm
Copy link
Member

wooorm commented Oct 19, 2023

Thank you!

@wooorm wooorm added 💪 phase/solved Post is done and removed 🤞 phase/open Post is being triaged manually labels Oct 19, 2023
kodiakhq bot pushed a commit to X-oss-byte/Nextjs that referenced this pull request Oct 24, 2023
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@mdx-js/loader](https://mdxjs.com) ([source](https://togithub.com/mdx-js/mdx)) | [`2.3.0` -> `3.0.0`](https://renovatebot.com/diffs/npm/@mdx-js%2floader/2.3.0/3.0.0) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@mdx-js%2floader/3.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@mdx-js%2floader/3.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@mdx-js%2floader/2.3.0/3.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@mdx-js%2floader/2.3.0/3.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) |
| [@mdx-js/react](https://mdxjs.com) ([source](https://togithub.com/mdx-js/mdx)) | [`2.3.0` -> `3.0.0`](https://renovatebot.com/diffs/npm/@mdx-js%2freact/2.3.0/3.0.0) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@mdx-js%2freact/3.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@mdx-js%2freact/3.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@mdx-js%2freact/2.3.0/3.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@mdx-js%2freact/2.3.0/3.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>mdx-js/mdx (@&#8203;mdx-js/loader)</summary>

### [`v3.0.0`](https://togithub.com/mdx-js/mdx/releases/tag/3.0.0)

[Compare Source](https://togithub.com/mdx-js/mdx/compare/2.3.0...3.0.0)

(see <https://mdxjs.com/migrating/v3/> on how to migrate)

##### Change

-   [`e08b759`](https://togithub.com/mdx-js/mdx/commit/e08b7596) [`5afa48e`](https://togithub.com/mdx-js/mdx/commit/5afa48e6) Change to require Node 16
-   [`5a13d73`](https://togithub.com/mdx-js/mdx/commit/5a13d73b) Change to use export maps
-   [`cbc2822`](https://togithub.com/mdx-js/mdx/commit/cbc2822f) Update `unified`, types, plugins, etc
-   [`96b51f9`](https://togithub.com/mdx-js/mdx/commit/96b51f93) Remove inferral of development from `NODE_ENV`

##### Change (unlikely to affect you)

-   [`c961af8`](https://togithub.com/mdx-js/mdx/commit/c961af80) Remove `useDynamicImport` option
-   [`9cb26fd`](https://togithub.com/mdx-js/mdx/commit/9cb26fd1) `@mdx-js/register`: remove package
-   [`0d1558a`](https://togithub.com/mdx-js/mdx/commit/0d1558a3) `@mdx-js/esbuild`: remove experimental `allowDangerousRemoteMdx`
-   [`0f62bce`](https://togithub.com/mdx-js/mdx/commit/0f62bce9) `@mdx-js/node-loader`: remove `fixRuntimeWithoutExportMap`
-   [`4f92422`](https://togithub.com/mdx-js/mdx/commit/4f924227) `@mdx-js/preact`: remove deprecated `MDXContext`, `withMDXComponents`
-   [`a362bb4`](https://togithub.com/mdx-js/mdx/commit/a362bb43) `@mdx-js/react`: remove deprecated `MDXContext`, `withMDXComponents`

##### Add

-   [`e12f307`](https://togithub.com/mdx-js/mdx/commit/e12f3079) Add support for passing `baseUrl` when running
-   [`2c511a4`](https://togithub.com/mdx-js/mdx/commit/2c511a40) Add support for `baseUrl` as a `URL`
-   [`1863914`](https://togithub.com/mdx-js/mdx/commit/1863914c) Add deprecation warning for classic runtime
-   [`a34177c`](https://togithub.com/mdx-js/mdx/commit/a34177c3) Add support for ES2024 in MDX, adjacent JSX and expression blocks
-   [`44fd9ca`](https://togithub.com/mdx-js/mdx/commit/44fd9cac) Add support for `await` in MDX
-   [`3a7f194`](https://togithub.com/mdx-js/mdx/commit/3a7f1947) Add `tableCellAlignToStyle` option, to use `align`
-   [`fdfe17b`](https://togithub.com/mdx-js/mdx/commit/fdfe17b8) `@mdx-js/rollup`: add support for Vite development mode
    by [@&#8203;remcohaszing](https://togithub.com/remcohaszing) in [mdx-js/mdx#2376

##### Misc

-   [`f48d038`](https://togithub.com/mdx-js/mdx/commit/f48d038b) Remove unneeded pragma comment after transform
-   [`8f3b292`](https://togithub.com/mdx-js/mdx/commit/8f3b2920) Add a `use strict` directive to function bodies
-   [`172e519`](https://togithub.com/mdx-js/mdx/commit/172e5190) `@mdx-js/react`: fix to classify `@types/react` as a peer dependency
    by [@&#8203;remcohaszing](https://togithub.com/remcohaszing) in [mdx-js/mdx#2281
-   [`a7bd79b`](https://togithub.com/mdx-js/mdx/commit/a7bd79bb) Refactor output to immediately export default
-   [`e525db9`](https://togithub.com/mdx-js/mdx/commit/e525db9b) [`dae82ae`](https://togithub.com/mdx-js/mdx/commit/dae82ae4) Refactor some errors
-   [`ce173f2`](https://togithub.com/mdx-js/mdx/commit/ce173f28) Refactor to add types for JSX runtimes
-   [`8a56312`](https://togithub.com/mdx-js/mdx/commit/8a563128) Refactor output to use spread, not `Object.assign`
    by [@&#8203;remcohaszing](https://togithub.com/remcohaszing) in [mdx-js/mdx#2328
-   [`825717f`](https://togithub.com/mdx-js/mdx/commit/825717fd) Refactor to sort default components
    by [@&#8203;remcohaszing](https://togithub.com/remcohaszing) in [mdx-js/mdx#2318
-   [`d8a62d2`](https://togithub.com/mdx-js/mdx/commit/d8a62d20) Add missing type dependencies
    by [@&#8203;arcanis](https://togithub.com/arcanis) in [mdx-js/mdx#2256

##### Docs

-   [`a9f0c04`](https://togithub.com/mdx-js/mdx/commit/a9f0c046) Add guide on injecting components
-   [`24e3d8d`](https://togithub.com/mdx-js/mdx/commit/24e3d8d1) Add compat sections to readmes
-   [`30e4a5d`](https://togithub.com/mdx-js/mdx/commit/30e4a5d5) Add sponsor
-   [`07503a5`](https://togithub.com/mdx-js/mdx/commit/07503a5f) Update link to KaTeX CSS in docs
    by [@&#8203;victor23k](https://togithub.com/victor23k) in [mdx-js/mdx#2360
-   [`74aee56`](https://togithub.com/mdx-js/mdx/commit/74aee569) [`bc1d9e5`](https://togithub.com/mdx-js/mdx/commit/bc1d9e56) [`765310c`](https://togithub.com/mdx-js/mdx/commit/765310ca) [`6d1e64d`](https://togithub.com/mdx-js/mdx/commit/6d1e64d9) Refactor docs
-   [`7fd1d9a`](https://togithub.com/mdx-js/mdx/commit/7fd1d9a4) Fix docs on how to use solid
    by [@&#8203;BeiyanYunyi](https://togithub.com/BeiyanYunyi) in [mdx-js/mdx#2300
-   [`4129f90`](https://togithub.com/mdx-js/mdx/commit/4129f90e) Fix a couple typos
    by [@&#8203;deining](https://togithub.com/deining) in [mdx-js/mdx#2266
-   [`bb902f8`](https://togithub.com/mdx-js/mdx/commit/bb902f83) Fix typo
    by [@&#8203;ChristianMurphy](https://togithub.com/ChristianMurphy) in [mdx-js/mdx#2380

##### Site

-   [`78a1eb5`](https://togithub.com/mdx-js/mdx/commit/78a1eb52) Add v3 blog post
-   [`2b1948c`](https://togithub.com/mdx-js/mdx/commit/2b1948c0) Add v3 migration guide
-   [`d6bb70c`](https://togithub.com/mdx-js/mdx/commit/d6bb70ca) Add improved error display in playground
-   [`89097e4`](https://togithub.com/mdx-js/mdx/commit/89097e4c) Remove unmaintained dev-dependency
-   [`3e23ba9`](https://togithub.com/mdx-js/mdx/commit/3e23ba90) Add more options to playground
-   [`d92128b`](https://togithub.com/mdx-js/mdx/commit/d92128ba) Fix links in docs
-   [`ab3aa96`](https://togithub.com/mdx-js/mdx/commit/ab3aa966) Add GitHub pages
    by [@&#8203;remcohaszing](https://togithub.com/remcohaszing) in [mdx-js/mdx#2377
-   [`a2c8693`](https://togithub.com/mdx-js/mdx/commit/a2c86936) Fix site
    by [@&#8203;wooorm](https://togithub.com/wooorm) in [mdx-js/mdx#2358
-   [`dbe9f44`](https://togithub.com/mdx-js/mdx/commit/dbe9f44b) Fix playground AST views w/ clone
    by [@&#8203;Jokcy](https://togithub.com/Jokcy) in [mdx-js/mdx#2315
-   [`7504cfb`](https://togithub.com/mdx-js/mdx/commit/7504cfb8) Add more options to playground, directives, format, etc
    by [@&#8203;slorber](https://togithub.com/slorber) in [mdx-js/mdx#2295
-   [`57301df`](https://togithub.com/mdx-js/mdx/commit/57301dfa) Add resizable editor/layout to playground
    by [@&#8203;slorber](https://togithub.com/slorber) in [mdx-js/mdx#2296
-   [`9eb747d`](https://togithub.com/mdx-js/mdx/commit/9eb747d6) Add info on how to build site locally
    by [@&#8203;slorber](https://togithub.com/slorber) in [mdx-js/mdx#2297

**Full Changelog**: mdx-js/mdx@2.3.0...3.0.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these updates again.

---

 - [ ] If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/X-oss-byte/Nextjs).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 area/deps This affects dependencies ☂️ 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.

None yet

3 participants