Skip to content

Fix ToImgopts/DownloadImgopts type definitions - #8058

Closed
hikmetba-bit wants to merge 1 commit into
plotly:mainfrom
hikmetba-bit:docs/fix-toimage-downloadimage-type-defs
Closed

hikmetba-bit wants to merge 1 commit into
plotly:mainfrom
hikmetba-bit:docs/fix-toimage-downloadimage-type-defs

Conversation

@hikmetba-bit

Copy link
Copy Markdown

Summary

Fixes #8057.

Both ToImgopts and DownloadImgopts required format/width/height (and DownloadImgopts also required filename), but the runtime defaults every one of them:

  • src/plot_api/to_image.js: opts = opts || {}toImage()/downloadImage() can be called with no options object at all. format defaults to 'png', scale to 1, imageDataOnly to false; width/height fall back to the graph's own layout dimensions when omitted.
  • src/snapshot/download.js: explicitly sets opts.format = opts.format || 'png', opts.width = opts.width || null, opts.height = opts.height || null before use, and derives filename from the graph title/subtitle (falling back to 'plot-image') when not given.

Also found two gaps against the runtime's shared attrs object in to_image.js while tracing this:

  • format's value union was missing 'full-json' (valType: 'enumerated', values: [..., 'full-json'], dflt: 'png' — an exportable format since v1.53.0, per the issue).
  • imageDataOnly (valType: 'boolean', dflt: false) had no type at all.

Added setBackground to both interfaces too: to_image.js's shared attrs object accepts it (valType: 'any'), and it's the same per-call override already typed for the top-level Config#setBackground in this same file ('opaque' | 'transparent' | ((gd, bgColor) => void)) — reused that exact type for consistency.

Testing

  • npm run typecheck (tsc --noEmit, project-wide): clean, both before and after this change (no existing test exercised these two interfaces).
  • Wrote a standalone positive-verification file exercising the fixed cases ({} for both interfaces, imageDataOnly: true, format: 'full-json', setBackground: 'opaque') and typechecked it directly with tsc --strict: passes cleanly against this change.
  • Confirmed the same file fails to compile against the original config.d.ts with exactly the errors the issue describes (TS2739 missing required properties, TS2353 unknown property imageDataOnly, TS2322 'full-json' not assignable) — mutation-tested by stashing just this file.

🤖 Generated with Claude Code

Both interfaces required format/width/height (and DownloadImgopts also
required filename), but the runtime (src/plot_api/to_image.js,
src/snapshot/download.js) defaults every one of them -- toImage/
downloadImage can be called with no options at all (opts = opts || {}).

Also two gaps against the runtime's attrs object in to_image.js:

- format's value union was missing 'full-json' (documented export
  format, valType enumerated values includes it, dflt: 'png').
- imageDataOnly (valType boolean, dflt: false) had no type at all.

Added setBackground to both interfaces too: to_image.js's shared attrs
object accepts it (valType 'any', same per-call meaning as the existing
top-level Config#setBackground override in this same file), but neither
interface had it typed.

Fixes plotly#8057.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@camdecoster

Copy link
Copy Markdown
Contributor

We haven't decided on an approach yet, so I'm closing this PR. You're welcome to join the discussion in the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: imperfect type definitions around graph export features

2 participants