Skip to content

RFC 11: Typed closure parameters and type arguments at a call #12

Open

Description

@yumemi-thomas

One of a series of small proposals, in order; the tracking issue lists them. Depends on: none beyond upstream PR solidjs#69.

Summary

Let an arrow function's parameter carry a TypeScript type, (v: string) => …, and let a call carry TypeScript type arguments, createStore<T>(…). Both are TypeScript's alone: kept in the projection (the TypeScript file rx-tsc generates from an .rx module to type-check it), dropped from the compiled JavaScript, and never read by the .rx checker.

Motivation

Two common library shapes leave TypeScript nothing to infer from.

  • debounce from @solid-primitives/scheduled types the arguments of the function it wraps as unknown. In search = debounce((v) => { query := v }, 150), writing v into a string source fails the type check, and there is no way on upstream to say what v is.
  • Solid 2's createStore is generic in its initial value. createStore({ items: [], done: 0 }) infers items: never[], so every later write of a string into items is a type error. TypeScript's answer is createStore<Todos>(…), which upstream's parser does not accept.

Upstream's expression grammar is TypeScript's with removals (07 §3); these two forms were not in it.

Proposal

07 §3, two additions:

  • A typed arrow parameter. An arrow's parameter may carry a TypeScript type. It is checked in the projection and dropped from the JavaScript. := after a name is still a write, never a type.
  • Type arguments at a call. A call may carry type arguments, written right after the callee and followed by (. They exist for a generic whose argument leaves nothing to infer. a < b stays a comparison: a <…> is type arguments only when it is balanced, its < comes directly after a name or member, its > is followed directly by (, and what it holds can be a type: with &&, ||, ==, !=, <=, >=, *, /, %, !, ^ or ~ outside a string literal it is not, so a<b && c>(d) is two comparisons, as TypeScript reads it; nor, outside any bracket, with a +, a - that is not a negative number's, an = that is not an arrow's, a ? that no extends precedes, or a call (f(x)), so a<b + c>(d) and a<b ? c : d>(d) are comparisons too. The list may span lines, as a record type often does.
import { debounce } from "./prims.js"
source query: string := ""
export default component App() {
  search = debounce((v: string) => { query := v }, 150)
  <input onInput={(e) => { search(e.target.value) }} />
  <p>{query}</p>
}
import { createStore } from "./store.js"
export default component Todos() {
  [todos, setTodos] = createStore<{ items: string[]; done: number }>({ items: [], done: 0 })
  <p>{todos.done} of {len(todos.items)} done</p>
}

The .d.rx side (a .d.rx is the declaration file that describes a JS module to .rx) already writes the generic, export pure fn createStore<T>(init: T): …. Type parameters there are TypeScript text that the projection passes on unchanged.

Neither form changes what the checker sees. A typed parameter is still a value, and the call is still the same call. The types matter only in the build's type check, where a wrong one is TypeScript's error on that line. For example, debounce((v: number) => { query := v }) is TS2345, because a write is the source's setter called with the value.

Drawbacks and alternatives

  • .rx keeps one more piece of TypeScript syntax. The alternative, typing through a declaration (a wrapper .d.rx per call shape), moves the type away from the call and does not work for a generic store whose type is the app's.
  • f<T>(x) has the usual ambiguity with comparisons. The rule above is narrower than TypeScript's: f <T>(x) with a space, or f<T> (x), is read as comparisons. A review found it was also wider: "balanced" accepted a<b && c>(d) and compiled it to a()(d()), a silent change of meaning; the contents must now be able to be a type. The first prototype also refused a list that spans lines; a review pointed out that a record type is usually written over several, so that limit was dropped.
  • A cast ([] as string[]) would cover createStore but not the closure parameter. Neither is in the grammar.

Prototype

Branch claude/magical-bell-pfhb1w of yumemi-thomas/reactivescript:

  • 9a8a7c9 adds typed closure parameters: both parsers and both projections, and examples/primitives uses one.
  • 23063b8 adds type arguments at a call (typeArgsAt in ref/src/parse.ts, type_args_at in compiler/src/parse.rs), and examples/formisch writes createStore<…>(…).
  • 0b59b16 lets the list span lines.
  • 3a6f24b requires the contents to be able to be a type (looksLikeTypes, looks_like_types).

Spec: 07 §3. Scenarios: S-module-20 (a typed parameter is accepted; (v: number) writing a string source is TS2345) and S-module-25 (createStore<{ done: number; items: string[] }>(…), on one line and over several), S-surface-7 (a<b && c>(d) is comparisons; its frames run on every runner). compiler/test/formisch.mjs builds the store example and drives it in Chromium. Both implementations (ref/ and the Rust crate) agree.

  • f031b0b extends that test after a further review found a<b + c>(d), a<b - c>(d), a<b ? c : d>(d) and a<f(b)>(d) still compiled to a(d); S-surface-7 runs the conditional form on every runner.

Revised after the second review (2026-11-21, cdc00d4)

RFC 11, after the second review. The operator list is gone: type arguments are recognised only when a TypeScript type grammar consumes the whole <…> list (references with arguments, unions, intersections, indexed access, arrays, tuples, object types with members ending at ,, ; or a line break, function and constructor types, conditional types, keyof, typeof, infer, literal and template types). a<b[c+1]>(d) is (a < b[c + 1]) > d again, as on pr69 and in TypeScript. Both parsers share the grammar token for token; unit cases in parse.rs, S-surface-7.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions