Repository navigation
feat(vgplot): Convert Mosaic vgplot to TypeScript - #1394
dangotbanned wants to merge 32 commits into
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
| { | ||
| "name": "mosaic-esm-examples", | ||
| "version": "0.35.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "typecheck": "tsc --build" | ||
| }, | ||
| "dependencies": { | ||
| "@uwdata/vgplot": "workspace:^" | ||
| } | ||
| } No newline at end of file |
There was a problem hiding this comment.
This is the vgplot equivalent of what I did for Mosaic Spec:
I feel quite strongly that these files need to be type-checked. I'd made so many mistakes in this PR 1 and they were not caught without the typing coverage provided by them.
I think it would be simpler to just write them to vgplot's test suite though?
- We'd avoid the need for this packaging boilerplate
- Could also measure runtime coverage
- The location of
specs/esm/seems not that important, since everything is duplicated todocs/public/specs/esm/anyway 🤔
Footnotes
-
beyond what's visible from the commits ↩
There was a problem hiding this comment.
In VSCode, the References CodeLens now has the information required to link between vgplot and usage in specs/esm
cf978e1 to
0421905
Compare
| //@ts-expect-error A spread argument must either have a tuple type or be passed to a rest parameter. | ||
| export const area = (...args) => mark('area', ...args); | ||
| //@ts-expect-error | ||
| export const areaX = (...args) => mark('areaX', ...args); |
There was a problem hiding this comment.
I think resolving these issues in a meaningful way, is out of scope for the PR - but is something we need to do.
The wrong solution would be changing the signature of mark, {explicit,implicit}Type to accept ...args: any[] - which silences the errors without improving the UX.
Problem
Simply put, this signature provides no information to the caller - while being a core part of the vgplot API:
export const line = (...args) => mark('line', ...args);If I'm incredibly persistent, I could leave my editor and:
- Visit the Marks API Reference
- Scroll all the way down to vgplot/marks#Line
- Find out we don't provide this information at all, then follow through to the Observable Plot
linedocs - Scroll all the way to the bottom again to plot/marks/line#line-options
- Find out that this still doesn't tell me the signature, get redirected again to standard mark options
- Accept these single line descriptions that don't have types 😔
Does it have to be this way?
Observable Plot looks very different.
The JS definition of line already tells me much more:
export function line(data, {x, y, ...options} = {}) {
[x, y] = maybeTuple(x, y);
return new Line(data, {...options, x, y});
}But only that's only untyped because there's a marks/line.d.ts too.
This is what I actually want.
I don't care about fixing the @ts-expect-errors. I do care that they highlight a design choice that makes the UX worse than I know it can be 🙏
| if (!(typeof maybePlot === 'string')) { | ||
| throw new TypeError(`Unrecognized 'for' type: ${typeof maybePlot}`) | ||
| } |
There was a problem hiding this comment.
On main, there are 3 branches here for the type of for.
Show vgplot/vgplot/src/plot/legends.js#L8-L15
mosaic/packages/vgplot/vgplot/src/plot/legends.js
Lines 8 to 15 in ddf3e79
We don't have coverage for anything besides string.
It is the only documented option in the API ref and this matches the spec definition too:
Show vgplot/spec/src/spec/PlotLegend.ts#L64-L69
mosaic/packages/vgplot/spec/src/spec/PlotLegend.ts
Lines 64 to 69 in ddf3e79
Throwing here was only meant to explain the change.
Of all the places that could validate types - I don't think it's needed when we aren't dealing with a union
| // TODO @dangotbanned: `options` might be expressible as a conditional type | ||
| function legend(channel: 'color' | 'opacity' | 'symbol', options: LegendForOptions): HTMLDivElement | ||
| function legend(channel: 'color' | 'opacity' | 'symbol', options: LegendOptions): PlotDirective | ||
| function legend(channel: 'color' | 'opacity' | 'symbol', options: Options = {}): HTMLDivElement | PlotDirective { |
There was a problem hiding this comment.
It might save a few lines from the duplicated overloads - but I'm not sure the result would be as readable
All of the others would have this too, but they also have other errors (#1394 (comment))
6eaec25 to
3e9d7a7
Compare
| interface Options { | ||
| filterBy?: mc.Selection; | ||
| optimize?: boolean; | ||
| } |
There was a problem hiding this comment.
AFAICT, it isn't really documented anywhere - but we have quite a lot of code in vgplot/plot/src/marks/Marks.ts that supports an unnest option too.
There's an example of it here:
I suppose you could call this documenting it 😉
mosaic/packages/vgplot/plot/src/marks/Mark.js
Lines 134 to 141 in 3cc45be
There was a problem hiding this comment.
There's probably something I'm missing, but this seems an odd form of special-casing:
I can see 2 more general features:
- Add an
unnestmethod toQueryorSelectQueryinmosaic-sql
i. Seepolars.LazyFrame.unnest1.
I think that has a better user-facing API than if you were to write it usingEXCLUDE2 - Accept 3 query operators as a
sourcefor marks invgplot
These two look pretty much the same, but the second one could allow queries that uses things besides where and unnest 😄
function current($filter: vg.Selection) {
return vg.plot(
vg.barX(vg.from('events', { filterBy: $filter, unnest: 'tags' }), {
x: vg.count(),
y: 'tags'
}),
vg.toggleY({ as: $filter })
);
}
function proposed($filter: vg.Selection) {
return vg.plot(
vg.barX(Query.from('events').where($filter).unnest('tags'), {
x: vg.count(),
y: 'tags'
}),
vg.toggleY({ as: $filter })
);
}Footnotes
-
Polars uses
unnestfor Struct andexplodefor List - whereas DuckDB usesunnestfor both ops ↩ -
The gaia example uses
EXCLUDEinside a string, but we haven't got an expression form yet ↩ -
possibly a limited selection of ↩
There was a problem hiding this comment.
- Accept query operators as a
sourcefor marks invgplot
Another use-case for that would be aligning 2 ways you can specify "inline" data.
We have one example that uses vg.loadObjects, which is wrapping a CreateQuery
But marks can also take data "directly", at the cost of interactivity.
I haven't seen an example that uses that feature
Lines 104 to 105 in 3cc45be
If marks accepted query nodes - the output of vg.loadObjects would work - and the only magic is piping that into a coordinator.
I'm assuming that would avoid the interactivity issue?
The other aspect is that Mark is complicated by needing to handle vg.from and arbitrary data 😅
42f6c84 to
449c6e1
Compare
Similar to #1361, but `vgplot` instead of `spec`
Changes the signature of and stops exporting `concat`
Now that *concat` is typed, the `Callback` alternative failed Related (f351d60)
This "allowed" `spec/ast` to pass in the wrong shape but the source is fixed now and unpacks correctly
Can finish the rest another time, but they all have a reasonable upper bound now
It is quite confusing that "options" aren't optional
All optional/required are now correct
449c6e1 to
ec93b63
Compare
Closes #1389
Description
This PR goes a few steps beyond renaming
*.jsfiles.The majority of
srcand all oftesthas pretty rich parameter types and everything has a useful return type.Highlights
Here's a bunch of screenshots
Directive !== Component
We now warn when directives are used in place of components (or the reverse).
This even includes
*Legendfunctions which can return either kind - depending onfor: ...:Selection !== Param
From (#1264)
I was working on interactors back when I noticed that.
Now that I'm typing the implementation - it looks like the only interactor that anticipates a regular
ParamisNearest.So we now report an error on
as: Paramfor everything butnearest*:Attributes
attributes.jsdefined 200+ single-parameter functions which had no typing whatsoever.In
attributes.ts, they all have a return type ofPlotDirectiveand I've made a start on typing each parameter too.By redefining
attrfas a generic function with a permissive default - we can now gradually constrain what is allowed at the definition site.So far I've only done this for a handful of cases, since I'm manually translating from
spec/PlotAttribute.ts.But it works! and the rest can be tightened up in a follow-up
Checked ESM examples
Most of the above have been screenshots from
specs/esm/*.js. For more details see (#1394 (comment)).