Skip to content

feat(ast): Add types for params, selection, transforms - #1374

Open
dangotbanned wants to merge 16 commits into
spec/ast-typing-canaryfrom
spec/ast-typing-2
Open

dangotbanned wants to merge 16 commits into
spec/ast-typing-canaryfrom
spec/ast-typing-2

Conversation

@dangotbanned

@dangotbanned dangotbanned commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Part 2 of #1364, covering another chunk of src/ast

Related

@dangotbanned
dangotbanned added this pull request to stack #1375 November 1, 2026 21:15
Comment on lines +80 to +82
export function mapTuple<T1, T2, R>(tuple: [T1, T2], fn: (v: T1 | T2) => R): [R, R] {
return tuple.map(fn) as [R, R];
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array.map doesn't preserve the shape of an array, which is an issue for all of the 2-tuple parts of the api

Comment on lines +85 to +94
/**
* 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);
}

@dangotbanned dangotbanned Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@dangotbanned dangotbanned added javascript Pull requests that update Javascript code spec Related to Mosaic Spec labels Nov 1, 2026
Comment on lines +13 to +18
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);
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OptionsNode.filter is currently showing up as unreferenced.
There might be something that's not typed yet but is using it though

dangotbanned added a commit that referenced this pull request Nov 2, 2026
- `string` was being passed into `parseTransform`
- `undefined` progagated from there without checking it

#1374 (comment)
Comment on lines +161 to +162
//@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)));

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +15 to +17
/* 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<

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed all of these errors that are still in the other branch

export class ParamNode extends ASTNode<mc.Param<unknown>, string, unknown> {
readonly value: Value | undefined;
readonly date: string | undefined;
constructor(value?: Value, date?: string) {
super(PARAM);
this.value = value;
this.date = date;
}
instantiate(ctx: InstantiateContext): mc.Param<unknown> {
const { date, value } = this;
const { Param } = ctx.api;
return isArray(value)
? // @ts-expect-error `v` is possibly null
Param.array(value.map((v) => v.instantiate(ctx)))
: Param.value<unknown>(isoparse(date, value));
}
codegen(ctx: CodegenContext): string {
const { value, date } = this;
const prefix = `${ctx.ns()}Param.`;
// oxlint-disable-next-line no-nested-ternary
return isArray(value)
? // @ts-expect-error `v` is possibly null
`${prefix}array([${value.map((v) => v.codegen(ctx)).join(', ')}])`
: // oxlint-disable-next-line unicorn/no-nested-ternary
date
? `${prefix}value(new Date(${JSON.stringify(date)}))`
: `${prefix}value(${JSON.stringify(value)})`;
}
// oxlint-disable-next-line typescript/explicit-module-boundary-types typescript/explicit-function-return-type
toJSON(): unknown {
const { date, value } = this;
// @ts-expect-error `v` is possibly null
// oxlint-disable-next-line no-nested-ternary
return isArray(value) ? value.map((v) => v.toJSON()) : date ? { date } : value;
}
}

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

@dangotbanned
dangotbanned marked this pull request as ready for review November 2, 2026 17:22
dangotbanned added a commit that referenced this pull request Nov 2, 2026
@dangotbanned
dangotbanned force-pushed the spec/ast-typing-2 branch 6 times, most recently from c0b3dbc to 931f088 Compare November 4, 2026 13:15
@dangotbanned
dangotbanned force-pushed the spec/ast-typing-2 branch 6 times, most recently from 1fa2969 to 04d528a Compare November 6, 2026 22:35
@dangotbanned
dangotbanned force-pushed the spec/ast-typing-2 branch 6 times, most recently from 421645f to cd4f8a7 Compare November 20, 2026 15:44

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

javascript Pull requests that update Javascript code spec Related to Mosaic Spec

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant