Skip to content

Commit b0f48b2

Browse files
committed
Address PR feedback
1 parent 166aa66 commit b0f48b2

4 files changed

Lines changed: 37 additions & 28 deletions

File tree

‎src/types/core/api.d.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ export function relayout(root: Root, layout: LayoutUpdate): Promise<PlotlyHTMLEl
221221
* @param val - New value of the attribute
222222
*/
223223
export function relayout(root: Root, astr: string, val: any): Promise<PlotlyHTMLElement>;
224+
/**
225+
* Update layout properties on an existing plot with either call form.
226+
*
227+
* This signature lets a wrapper read the parameter types with `Parameters<typeof relayout>`.
228+
* It does not check that the value matches the form.
229+
*/
230+
export function relayout(root: Root, astr: string | LayoutUpdate, val?: any): Promise<PlotlyHTMLElement>;
224231
/** Re-render the plot at `root` from its current data/layout. */
225232
export function redraw(root: Root): Promise<PlotlyHTMLElement>;
226233
/** Remove a plot and its event listeners from the DOM. */
@@ -240,6 +247,18 @@ export function restyle(root: Root, aobj: DataUpdate, traces?: number[] | number
240247
* @param traces - Trace index/indices to update (defaults to all)
241248
*/
242249
export function restyle(root: Root, astr: string, val: any, traces?: number[] | number): Promise<PlotlyHTMLElement>;
250+
/**
251+
* Update trace properties (style) on the existing plot with either call form.
252+
*
253+
* This signature lets a wrapper read the parameter types with `Parameters<typeof restyle>`.
254+
* It does not check that the arguments match the form.
255+
*/
256+
export function restyle(
257+
root: Root,
258+
astr: string | DataUpdate,
259+
val?: any,
260+
traces?: number[] | number
261+
): Promise<PlotlyHTMLElement>;
243262

244263
/**
245264
* Update both trace and layout properties in a single call.

‎src/types/core/events.d.ts‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,12 @@ export interface PlotRestyleEventUpdate {
150150
export type PlotRestyleEvent = [PlotRestyleEventUpdate, number[]];
151151

152152
/**
153-
* Payload for `plotly_relayout`. Carries a `LayoutUpdate`, so any attribute
154-
* path is a valid key, plus typed values for a few common axis-range and
155-
* autorange keys that flatten nested updates.
153+
* Payload for `plotly_relayout`. The keys are layout properties or attribute
154+
* paths of the changed values, for example `'xaxis.range[0]'`. A value behind an
155+
* attribute path has the type `any`, because a range value can be a number or a
156+
* date string.
156157
*/
157-
export interface PlotRelayoutEvent extends LayoutUpdate {
158-
/** New x-axis range minimum, when the user changed the range. */
159-
'xaxis.range[0]'?: number;
160-
/** New x-axis range maximum, when the user changed the range. */
161-
'xaxis.range[1]'?: number;
162-
/** New y-axis range minimum, when the user changed the range. */
163-
'yaxis.range[0]'?: number;
164-
/** New y-axis range maximum, when the user changed the range. */
165-
'yaxis.range[1]'?: number;
166-
/** True when the user toggled x-axis autoranging on. */
167-
'xaxis.autorange'?: boolean;
168-
/** True when the user toggled y-axis autoranging on. */
169-
'yaxis.autorange'?: boolean;
170-
}
158+
export type PlotRelayoutEvent = LayoutUpdate;
171159

172160
// ---------------------------------------------------------------------------
173161
// 3D scene / annotation / animation / legend events

‎test/types/relayout_event.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ import type { PlotRelayoutEvent } from '../../lib/index';
88

99
declare const event: PlotRelayoutEvent;
1010

11-
// Typed keys keep their value types
12-
const rangeStart: number | undefined = event['xaxis.range[0]'];
13-
const autorange: boolean | undefined = event['xaxis.autorange'];
14-
15-
// @ts-expect-error the typed range key holds a number, not a string
16-
const rangeStartAsString: string | undefined = event['xaxis.range[0]'];
11+
// A range value can be a number or a date string
12+
const numberRangeStart: number | undefined = event['xaxis.range[0]'];
13+
const dateRangeStart: string | undefined = event['xaxis.range[0]'];
1714

1815
// Any other attribute path is readable
1916
const secondAxisStart = event['xaxis2.range[0]'];

‎test/types/update_api.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Consumer-side checks for the `relayout`, `restyle` and `update` types.
33
*
4-
* Nothing here runs. `npm run typecheck` compiles this file against the public entry point.
4+
* Nothing here runs. `npm run test-types` compiles this file against the public entry point.
55
* A line marked `@ts-expect-error` must fail to compile, so the build breaks if a type becomes too permissive.
66
*/
77

@@ -27,8 +27,11 @@ Plotly.relayout(gd, 'annotations[0].text', 'hello');
2727
Plotly.relayout(gd, { titl: 'abc' });
2828
// @ts-expect-error wrong value type on a known top-level key
2929
Plotly.relayout(gd, { autosize: 'yes' });
30-
// @ts-expect-error the string form needs a value
31-
Plotly.relayout(gd, 'xaxis.autorange');
30+
31+
// A wrapper that reads the parameter types must see both call forms
32+
type RelayoutArg = Parameters<typeof Plotly.relayout>[1];
33+
export const relayoutObject: RelayoutArg = { autosize: true };
34+
export const relayoutPath: RelayoutArg = 'xaxis.autorange';
3235

3336
// ---------------------------------------------------------------------------
3437
// restyle
@@ -45,8 +48,10 @@ Plotly.restyle(gd, 'marker.color', ['red', 'blue'], [0, 1]);
4548
Plotly.restyle(gd, { colr: 'red' });
4649
// @ts-expect-error wrong value type on a known top-level key
4750
Plotly.restyle(gd, { visible: 'maybe' });
48-
// @ts-expect-error the string form needs a value
49-
Plotly.restyle(gd, 'marker.color');
51+
52+
type RestyleArg = Parameters<typeof Plotly.restyle>[1];
53+
export const restyleObject: RestyleArg = { visible: false };
54+
export const restylePath: RestyleArg = 'marker.color';
5055

5156
// ---------------------------------------------------------------------------
5257
// update

0 commit comments

Comments
 (0)