Repository navigation
feat(ast): Add types for params, selection, transforms - #1374
dangotbanned wants to merge 16 commits into
Conversation
| export function mapTuple<T1, T2, R>(tuple: [T1, T2], fn: (v: T1 | T2) => R): [R, R] { | ||
| return tuple.map(fn) as [R, R]; | ||
| } |
There was a problem hiding this comment.
Array.map doesn't preserve the shape of an array, which is an issue for all of the 2-tuple parts of the api
| /** | ||
| * Rewrap a {@link Set} or {@link Map} so it can be used as a typeguard. | ||
| * | ||
| * @returns A predicate function | ||
| */ | ||
| export function makeIsIn<T extends string>( | ||
| collection: ReadonlySet<T> | ReadonlyMap<T, unknown>, | ||
| ): (value: any) => value is T { | ||
| return (value: any): value is T => collection.has(value); | ||
| } |
There was a problem hiding this comment.
I've added this to reduce the pain of narrowing.
In python, most of the code would've been:
targets = {"a", "b", "c"}
okay: str = "b"
nope: str = "z"
if okay in targets:
# `okay` is narrowed `str` -> `Literal["a", "b", "c"]`
...
if nope in targets:
# this branch isn't reachable, but you can still check it without a type error
...The above is an (TSC) error when you use Set.has or Map.has (microsoft/TypeScript#60547)
4823cf3 to
6e91d41
Compare
| filter(predicate: (key: unknown, value: unknown) => boolean): OptionsNode { | ||
| const opt = Object.fromEntries( | ||
| Object.entries(this.options).filter(([key, value]) => predicate(key, value)), | ||
| ); | ||
| return new OptionsNode(opt); | ||
| } |
There was a problem hiding this comment.
OptionsNode.filter is currently showing up as unreferenced.
There might be something that's not typed yet but is using it though
6e91d41 to
bbccf5c
Compare
- `string` was being passed into `parseTransform` - `undefined` progagated from there without checking it #1374 (comment)
| //@ts-expect-error A spread argument must either have a tuple type or be passed to a rest parameter. | ||
| let expr = ctx.api[name](...args.map((a) => a.instantiate(ctx))); |
There was a problem hiding this comment.
This can only be fixed by a re-design.
There are 40+ functions on the other end of the splat, where both arity and supported types vary
| /* NOTE: Backwards-compat shim to keep `new ParamNode()` working. | ||
| * Would prefer this to be an abstract class, but it is exposed at the top-level.*/ | ||
| export class ParamNode< |
There was a problem hiding this comment.
I've fixed all of these errors that are still in the other branch
mosaic/packages/vgplot/spec/src/ast/ParamNode.ts
Lines 40 to 78 in 5851e66
If I were able to make ParamNode abstract, then 1 @ts-expect-error and a few lints would vanish.
Currently there's more code but errors are being raised for multiple kinds of invalid input.
There's also no branching after creating each ParamNode node subclass - which is where the null | undefineds were hiding before
394f497 to
f136988
Compare
f136988 to
b7585bb
Compare
c0b3dbc to
931f088
Compare
1fa2969 to
04d528a
Compare
421645f to
cd4f8a7
Compare
a9a611a to
209833e
Compare
Required a huge amount of work to avoid all the type errors - #1364 (comment) - #1364 (comment)
- `string` was being passed into `parseTransform` - `undefined` progagated from there without checking it #1374 (comment)
209833e to
694330f
Compare
Description
Part 2 of #1364, covering another chunk of
src/astRelated