Skip to content

Commit ed11c35

Browse files
authored
Merge pull request #7868 from plotly/cam/7867/generate-data-union-type
refactor: Generate union of data types
2 parents 065bab3 + f11b667 commit ed11c35

11 files changed

Lines changed: 97 additions & 136 deletions

File tree

src/types/ARCHITECTURE.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ The split:
5151
FontArray, ColorBar, HoverLabel, Domain, Pattern, TickFormatStops,
5252
LegendGroupTitle).
5353
- **Per-trace data interfaces** for all trace types (BarData, ScatterData,
54-
IndicatorData, etc.).
54+
IndicatorData, etc.), plus the **`Data` discriminated union** over all
55+
of them (`Partial<BarData> | Partial<ScatterData> | …`) — narrowed via
56+
the `type` field.
5557
- **Layout component interfaces** (LayoutAxis, Legend, Scene,
5658
Annotation, Shape, Slider, UpdateMenu, etc.) and the Layout
5759
interface itself.
@@ -85,7 +87,7 @@ This split is reflected in the types:
8587
| User-facing | Internal | Where defined |
8688
|---|---|---|
8789
| `Layout` | `FullLayout` | `Layout` in `generated/schema.d.ts`; `FullLayout` in `core/layout.internal.d.ts` |
88-
| `Data` (union over `type`) | `FullData` | `Data` in `core/data.d.ts` (union of schema `*Data` interfaces); `FullData = Data & FullDataInternals` in `core/data.internal.d.ts` |
90+
| `Data` (union over `type`) | `FullData` | `Data` in `generated/schema.d.ts` (union of schema `*Data` interfaces); `FullData = Data & FullDataInternals` in `core/data.internal.d.ts` |
8991
| (n/a) | `GraphDiv` (the `gd` param) | `core/graph-div.internal.d.ts` — DOM element with `_fullLayout`, `_fullData`, `calcdata`, etc. |
9092

9193
`FullData` is the discriminated union of schema trace types intersected with
@@ -106,7 +108,6 @@ src/types/
106108
├── core/ # hand-written types for the core API
107109
│ ├── api.d.ts # public API function signatures (newPlot, etc.)
108110
│ ├── config.d.ts # Config, ToImgopts (Edits re-exported from generated)
109-
│ ├── data.d.ts # Data union (over all schema `*Data` interfaces)
110111
│ ├── data.internal.d.ts # CalcData, FullData
111112
│ ├── events.d.ts # PlotMouseEvent, PlotlyHTMLElement, etc.
112113
│ ├── graph-div.internal.d.ts # GraphDiv, GraphContext
@@ -173,6 +174,8 @@ src/types/generated/schema.d.ts
173174
│ // Trace interfaces
174175
│ export interface ScatterData { marker?: _internal.Marker; ... }
175176
│ export interface BarData { ... }
177+
│ // Discriminated union over all traces
178+
│ export type Data = Partial<BarData> | Partial<ScatterData> | ...;
176179
│ // Layout
177180
│ export interface LayoutAxis { autorangeoptions?: _internal.AutoRangeOptions; ... }
178181
│ export interface Layout { ... }

src/types/GENERATOR.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ types into `src/types/generated/schema.d.ts`:
88
TransitionEasing, TraceType — plus a deprecated `PlotType` alias)
99
- Shared sub-interfaces (Font, ColorBar, HoverLabel, etc.)
1010
- Data interfaces for each trace type (BarData, ScatterData, IndicatorData, etc.)
11+
and the `Data` discriminated union over all of them
1112
- Layout component interfaces (LayoutAxis, Legend, Scene, Annotation, etc.)
1213
and the Layout interface itself
1314
- Animation / frame / edits interfaces (AnimationOpts, Frame, Edits)
@@ -90,7 +91,11 @@ so the automatic extractor skips them, but they need to be named for
9091
### Phase 3: Trace interfaces
9192

9293
Each trace gets an interface (`ScatterData`, `BarData`, etc.) whose
93-
properties reference shared types where fingerprints match.
94+
properties reference shared types where fingerprints match. After all
95+
trace interfaces are emitted, the generator also emits the discriminated
96+
union `Data = Partial<BarData> | Partial<BarpolarData> | …` covering every
97+
trace — narrowed via the `type` field. Adding or removing a trace in the
98+
schema flows through automatically.
9499

95100
### Phase 4: Layout types
96101

@@ -191,6 +196,7 @@ src/types/generated/schema.d.ts
191196
│ AutoRangeOptions,
192197
│ Lighting, ErrorY)
193198
├── Trace interfaces (ScatterData, BarData, ... — 49 traces)
199+
├── Data union (`Partial<*Data>` over every trace, discriminated by `type`)
194200
├── Layout component interfaces (LayoutAxis, Legend, Scene, Annotation, etc.)
195201
├── Layout interface
196202
└── Animation / frames / config (AnimationOpts, Frame, Edits, ConfigBase)

src/types/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ The following are **auto-generated from `plot-schema.json`** by
4848
- Common enum aliases (Calendar, Dash, AxisType, PatternShape, XRef, YRef,
4949
TransitionEasing, TraceType — and a deprecated `PlotType` alias)
5050
- Data interfaces for each trace type (BarData, ScatterData, IndicatorData, etc.)
51+
and the `Data` discriminated union over all of them
5152
- Layout component interfaces (LayoutAxis, Legend, Scene, Annotation,
5253
Shape, Slider, UpdateMenu, etc.) and the Layout interface itself
5354
- Shared sub-interfaces (Font, ColorBar, HoverLabel, LegendGroupTitle, etc.)

src/types/core/api.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
* Public API function types for Plotly.js
33
*/
44

5-
import type { AnimationOpts, Frame, Layout } from '../generated/schema';
5+
import type { AnimationOpts, Data, Frame, Layout } from '../generated/schema';
66
import type { Config, DownloadImgopts, ToImgopts } from './config';
7-
import type { Data } from './data';
87
import type { PlotlyHTMLElement } from './events';
98
import type { Icon, Template } from './layout';
109

src/types/core/data.d.ts

Lines changed: 0 additions & 124 deletions
This file was deleted.

src/types/core/events.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import type {
99
AnimationFrameOpts,
1010
Annotation,
11+
Data,
1112
Frame,
1213
Layout,
1314
LayoutAxis,
@@ -16,7 +17,6 @@ import type {
1617
} from '../generated/schema';
1718
import type { Datum } from '../lib/common';
1819
import type { Config } from './config';
19-
import type { Data } from './data';
2020

2121
// ---------------------------------------------------------------------------
2222
// Point / datum types in events

src/types/core/graph-div.internal.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
* Commonly referred to as `gd` in the codebase.
77
*/
88

9-
import type { Layout } from '../generated/schema';
9+
import type { Data, Layout } from '../generated/schema';
1010
import type { Config } from './config';
11-
import type { Data } from './data';
1211
import type { CalcData, FullData } from './data.internal';
1312
import type { FullLayout } from './layout.internal';
1413

src/types/core/layout.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
* literal utilities, and deprecated aliases.
99
*/
1010

11-
import type { Layout } from '../generated/schema';
12-
import type { Data, TraceType } from './data';
11+
import type { Data, Layout, TraceType } from '../generated/schema';
1312
import type { PlotlyHTMLElement } from './events';
1413

1514
// ---------------------------------------------------------------------------

src/types/generated/schema.d.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12091,6 +12091,65 @@ export interface WaterfallData {
1209112091
zorder?: number;
1209212092
}
1209312093

12094+
/**
12095+
* Union of every trace shape. All fields are optional via `Partial<…>` —
12096+
* narrow with the `type` discriminator before accessing trace-specific
12097+
* attributes:
12098+
*
12099+
* if (trace.type === 'bar') { trace.marker?.cornerradius }
12100+
* if (trace.type === 'pie') { trace.marker?.colors }
12101+
*/
12102+
export type Data =
12103+
| Partial<BarData>
12104+
| Partial<BarpolarData>
12105+
| Partial<BoxData>
12106+
| Partial<CandlestickData>
12107+
| Partial<CarpetData>
12108+
| Partial<ChoroplethData>
12109+
| Partial<ChoroplethmapData>
12110+
| Partial<ChoroplethmapboxData>
12111+
| Partial<ConeData>
12112+
| Partial<ContourData>
12113+
| Partial<ContourcarpetData>
12114+
| Partial<DensitymapData>
12115+
| Partial<DensitymapboxData>
12116+
| Partial<FunnelData>
12117+
| Partial<FunnelareaData>
12118+
| Partial<HeatmapData>
12119+
| Partial<HistogramData>
12120+
| Partial<Histogram2dData>
12121+
| Partial<Histogram2dcontourData>
12122+
| Partial<IcicleData>
12123+
| Partial<ImageData>
12124+
| Partial<IndicatorData>
12125+
| Partial<IsosurfaceData>
12126+
| Partial<Mesh3dData>
12127+
| Partial<OhlcData>
12128+
| Partial<ParcatsData>
12129+
| Partial<ParcoordsData>
12130+
| Partial<PieData>
12131+
| Partial<SankeyData>
12132+
| Partial<ScatterData>
12133+
| Partial<Scatter3dData>
12134+
| Partial<ScattercarpetData>
12135+
| Partial<ScattergeoData>
12136+
| Partial<ScatterglData>
12137+
| Partial<ScattermapData>
12138+
| Partial<ScattermapboxData>
12139+
| Partial<ScatterpolarData>
12140+
| Partial<ScatterpolarglData>
12141+
| Partial<ScattersmithData>
12142+
| Partial<ScatterternaryData>
12143+
| Partial<SplomData>
12144+
| Partial<StreamtubeData>
12145+
| Partial<SunburstData>
12146+
| Partial<SurfaceData>
12147+
| Partial<TableData>
12148+
| Partial<TreemapData>
12149+
| Partial<ViolinData>
12150+
| Partial<VolumeData>
12151+
| Partial<WaterfallData>;
12152+
1209412153
// ---------------------------------------------------------------------------
1209512154
// Layout component interfaces
1209612155
// ---------------------------------------------------------------------------

src/types/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export * from './lib/common';
1111
// Core types (public)
1212
export * from './core/api';
1313
export * from './core/config';
14-
export * from './core/data';
1514
export * from './core/events';
1615
export * from './core/layout';
1716

0 commit comments

Comments
 (0)