Skip to content

fix(types): support nested attribute path strings in relayout, restyle, and update - #8050

Open
CAOShurong wants to merge 2 commits into
plotly:mainfrom
CAOShurong:fix/relayout-typing-8047
Open

CAOShurong wants to merge 2 commits into
plotly:mainfrom
CAOShurong:fix/relayout-typing-8047

Conversation

@CAOShurong

Copy link
Copy Markdown
Contributor

Overview

In plotly.js runtime, Plotly.relayout, Plotly.restyle, and Plotly.update support updating nested properties using dot/bracket attribute paths (such as 'xaxis.autorange', 'xaxis.range[0]', 'annotations[0].text'), as well as the 2-parameter string invocation form Plotly.relayout(gd, 'xaxis.autorange', true).

With the v4.1 TypeScript definitions, calling relayout() with nested attribute path keys resulted in a type error ('xaxis.autorange' does not exist in type 'Partial<Layout>') because Layout lacked nested attribute path signatures, and only the 2-argument object overload was declared.

Changes

  1. tasks/generate_schema_types.mjs & src/types/generated/schema.d.ts:
    • Added common nested axis and title property types directly to Layout (xaxis.autorange, yaxis.autorange, xaxis.range, xaxis.type, xaxis.title, etc.) for IDE autocompletion and hover documentation.
    • Added template literal index signatures [key: ${string}.${string}]: any; and [key: ${string}[${string}]: any; to Layout, allowing any arbitrary dotted or indexed attribute paths while strictly preserving excess-property checks for typos on top-level layout keys (e.g. titl: 'abc' is still caught and rejected as a typo).
  2. src/types/core/api.d.ts:
    • Added 2-parameter string attribute path overload for relayout(root, astr, val).
    • Added 2-parameter string attribute path overload for restyle(root, astr, val, traces?).
    • Allowed Partial<Data> | Record<string, any> in restyle and update to support dotted attribute updates on traces.
  3. src/types/core/layout.d.ts & lib/index.d.ts:
    • Exported LayoutUpdate = Partial<Layout> type alias.

Verification

  • npm run typecheck (tsc --noEmit) passes with 0 errors.
  • npm run schema-typegen-diff-check passes with 0 errors (clean sync with generator).
  • npm run lint passes with 0 errors.
  • Verified that top-level typos (e.g. { titl: 'New Title' }) continue to be caught and rejected by TypeScript with error TS2561 / TS2353.

Fixes #8047.

…e, and update

In plotly.js runtime, Plotly.relayout, Plotly.restyle, and Plotly.update support updating nested properties using dotted attribute paths (e.g. 'xaxis.autorange', 'xaxis.range[0]', 'annotations[0].text') as well as the 2-parameter string invocation form (Plotly.relayout(gd, 'xaxis.autorange', true)).

- Update tasks/generate_schema_types.mjs to generate common nested axis and title properties alongside dotted/indexed template literal index signatures on Layout, while preserving strict excess-property checking on top-level layout typos.
- Add 2-parameter string attribute path overloads for relayout and restyle in api.d.ts.
- Allow Partial<Data> | Record<string, any> in restyle and update trace arguments.
- Export LayoutUpdate type alias in layout.d.ts and lib/index.d.ts.
- Regenerate src/types/generated/schema.d.ts.

Fixes plotly#8047
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]: Typing issue in relayout()

1 participant