Typings import `react` but `@types/react` is not declared as a peer dependency

Ouverte Adaptée aux débutants
#617 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
62/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
react, typescript

Piste de recherche

La correction concerne package.json, où peerDependencies liste react, react-dom et tinymce mais pas @types/react. Les typages publiés dans lib/es2015/main/ts/components/Editor.d.ts importent depuis react, ce qui explique la nécessité de cette déclaration. Reproduisez le problème avec le dépôt lié en exécutant pnpm install puis pnpm typecheck, avec enableGlobalVirtualStore défini sur true. La tâche est terminée lorsque la peer dépendance optionnelle @types/react est déclarée et que la vérification de types réussit avec ce paramètre.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

status: escalated

What is the current behavior?

The published typings import from react (for example lib/es2015/main/ts/components/Editor.d.ts), but package.json doesn't declare @types/react anywhere. The peer dependencies are only react, react-dom and tinymce.

This usually works by accident because @types/react is hoisted somewhere above the package in node_modules. It fails under package managers that only let a package see what it declares. With pnpm's global virtual store (enableGlobalVirtualStore: true), each package is linked from a shared directory outside the project, so TypeScript can't find @types/react from inside @tinymce/tinymce-react. Editor then stops typechecking as a React component:

src/App.tsx(4,10): error TS2607: JSX element class does not support attributes because it does not have a 'props' property.
src/App.tsx(4,11): error TS2786: 'Editor' cannot be used as a JSX component.
  Its type 'typeof Editor' is not a valid JSX element type.
    Type 'typeof Editor' is not assignable to type 'new (props: any, context: any) => Component<any, any, any>'.
      Type 'Editor' is missing the following properties from type 'Component<any, any, any>': context, setState, forceUpdate, props, state

This is a different cause from https://github.com/tinymce/tinymce-react/issues/332. That issue was two mismatched @types/react versions, while here the package can't resolve @types/react at all.

Please provide the steps to reproduce and if possible a minimal demo of the problem via codesandbox.io or similar.

Minimal reproduction: https://github.com/joeymckenzie/tinymce-react-types-react-repro

pnpm install
pnpm typecheck

src/App.tsx is just:

import { Editor } from "@tinymce/tinymce-react";

export function App() {
  return <Editor licenseKey="gpl" initialValue="<p>Hello</p>" />;
}

With the same lockfile and enableGlobalVirtualStore: false, tsc passes.

What is the expected behavior?

Editor typechecks without relying on @types/react being hoisted. Declaring it as an optional peer would do that and keep sharing the consumer's copy:

"peerDependencies": {
  "@types/react": "^19.0.0 || ^18.0.0 || ^17.0.0 || ^16.7.0"
},
"peerDependenciesMeta": {
  "@types/react": { "optional": true }
}

Our workaround in the meantime is declaring it on the package's behalf in pnpm-workspace.yaml:

packageExtensions:
  "@tinymce/tinymce-react":
    peerDependencies:
      "@types/react": "*"
    peerDependenciesMeta:
      "@types/react":
        optional: true

Which versions of TinyMCE, and which browser / OS are affected by this issue? Did this work in previous versions of TinyMCE or tinymce-react?

@tinymce/tinymce-react 6.3.0

Langage dominant
TypeScript
Étoiles
1.1k
Forks
167
Métriques de merge des PR
Aucune PR mergée en 30 j

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de tinymce/tinymce-react

Toutes les issues de tinymce/tinymce-react

Issues similaires

Plus d'issues TypeScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.