[BUG]: `ModeBar` generated type should be more specific
还没有人认领这个 Issue。
- 主要语言
- JavaScript
- 星标
- 18.3k
- 派生
- 2k
- 平均合并
- 2 天 12 小时
- 30 天内合并 PR
- 28
描述
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.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 src/types/generated/components/modebar.d.ts、src/components/modebar/attributes.ts 和 src/types/lib/attributes.d.ts 开始,追踪 ModeBar 属性如何变成 TypeScript 类型。在评估可能的实现方式之前,阅读 src/components/modebar/constants.js 以了解运行时按钮注册表。完成的标准是:add 和 remove 暴露预期的按钮名称类型,uirevision 为 number | string,并且 uid 存在,同时不丢失生成的类型结构。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, typescript
- 领域
- data-visualization
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 48/100