[BUG]: `ModeBar` generated type should be more specific
Personne n'a encore pris cette issue.
- Langage dominant
- JavaScript
- Étoiles
- 18.3k
- Forks
- 2k
- Merge moyen
- 2 j 12 h
- PR mergées (30 j)
- 28
Description
Description
The generated ModeBar type in src/types/generated/components/modebar.d.ts is less specific than the hand-written ModeBar in @types/plotly.js. The type generator faithfully maps valType: 'string' to string and valType: 'any' to any, but this loses semantic type information for the add, remove, and uirevision properties.
| Property | Generated type | Expected type (per DT) |
|---|---|---|
add |
string | string[] |
ModeBarDefaultButtons | ModeBarDefaultButtons[] |
remove |
string | string[] |
ModeBarDefaultButtons | ModeBarDefaultButtons[] |
uirevision |
any |
number | string |
uid |
missing | string |
Consumers lose autocomplete for valid modebar button names when using layout.modebar.add or layout.modebar.remove.
Steps to reproduce
- Use
layout.modebar.addin a TypeScript project importing from plotly.js - Observe that the type accepts any
stringinstead of suggesting valid button names like'lasso2d','select2d','zoom2d', etc.
Notes
Root cause: The add and remove attributes in src/components/modebar/attributes.ts use valType: 'string' with arrayOk: true. The type generation pipeline (ValTypeToTS in src/types/lib/attributes.d.ts) already supports narrowing StringAttr to a literal union when a values array is provided — but the modebar attributes don't provide one. The button names are dynamically computed at runtime in src/components/modebar/constants.js (foreButtons is built by iterating over the button registry), so they can't be passed as a static as const array.
Possible solutions:
-
Override the generated type in
layout.d.ts— Import the generatedModeBarand re-export a refined version usingOmit+ tighter property types. Keeps the generated base while tightening the public API. Low effort, no changes to the generator or attribute system. -
Inline static
valuesin the attribute definition — Addvalues: ['lasso2d', 'select2d', ...] as constdirectly to theaddandremoveattributes insrc/components/modebar/attributes.ts. The existingValTypeToTSconditional would then produce the union automatically. Downside: duplicates the button list (source of truth is the button registry, not this array). -
Make
foreButtonsstatically typed — Convertsrc/components/modebar/constants.jsto TypeScript with staticas constarrays instead of dynamically computing the list from the button registry. The attributes could then reference the const array asvalues. Downside: requires restructuring how buttons are registered and breaks the dynamic discovery pattern.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par src/types/generated/components/modebar.d.ts, src/components/modebar/attributes.ts et src/types/lib/attributes.d.ts afin de suivre la manière dont les propriétés de ModeBar deviennent des types TypeScript. Lisez src/components/modebar/constants.js pour comprendre le registre des boutons à l’exécution avant d’évaluer les approches possibles. C’est terminé lorsque add et remove exposent le type de nom de bouton prévu, que uirevision est number | string et que uid est présent sans perdre la structure de types générée.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, typescript
- Domaine
- data-visualization
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- Calme
- Clarté
- Plutôt claire
- Accessibilité débutants
- 48/100