Skip to content

typescript: type a callback parameter from context through an optional callback and without a staged lib - #1740

Merged
swapnilpaliwal-sd merged 2 commits into
0.1.8from
loop/ty-ts-contextual-lambda-param
Sep 29, 2026
Merged

swapnilpaliwal-sd merged 2 commits into
0.1.8from
loop/ty-ts-contextual-lambda-param

Conversation

@swapnilpaliwal-sd

@swapnilpaliwal-sd swapnilpaliwal-sd commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

TypeScript: type an arrow's parameter from its callback context when the callback is optional (p.then(u => u.m())) and when no standard library is staged (xs.map/forEach/filter/reduce/…, promise.then, Map.forEach). The type is read from the receiver's container or Promise reference, following type aliases and nested callbacks.

  • A program's own class Set<T> / class Promise<T> isn't read as the built-in: its callbacks keep their own parameter type.
  • A union of two different function types gives no contextual type, as tsc gives none.
  • Golden cases 80-contextual-lambda-param and 81-contextual-lambda-param-no-lib; both agree with tsc 35/35. Controls: user Set/Promise/map/then, union of callbacks, annotated params.
  • Checked: TS suite 91/91 on the VM. Probes (known edges): stress 13→34, receiver-typing 25→28, class-hierarchy 12→13. Two real TS apps have identical edges before and after, with indexing at 7–11s either way.

swapnilpaliwal-sd and others added 2 commits September 28, 2026 20:08
…l callback and without a staged lib

`p.then(u => u.m())` left `u` untyped: `then`'s callback parameter is written
`((value: T) => R) | undefined | null`, and the rule only looked for a bare function
type. Unwrap unions and parentheses to reach it.

With no standard library staged (a default index), `xs.map(u => u.m())`, `forEach`,
`filter`, `reduce` and `promise.then` had no declaration to read a signature from, so
the parameter stayed untyped while `for (const u of xs)` resolved. Type the element
parameter of the built-in element callbacks from the receiver's container reference,
and `then`'s value from a Promise/PromiseLike reference.

Co-authored-by: axiomcode-bot[bot] <334110751+axiomcode-bot[bot]@users.noreply.github.com>
…hrough aliases and nested callbacks

The no-library route typed a `forEach` / `then` callback parameter from the receiver's
type argument by NAME, so a program's own `class Set<T>` or `class Promise<T>`, whose
callbacks receive something else, got the type argument as well as its own parameter
type. A container name that resolves to a client declaration is no longer read as the
built-in.

A union of two different function types gives the compiler no contextual type; the
union is now looked through only when exactly one member is a function type.

The element now travels as a type reference, so the route also reaches
`type Users = User[]`, `Alias<User>` over `Array<T>`, a callback over a parameter that
was itself typed this way (`xss.forEach(xs => xs.forEach(...))`,
`p.then(us => us.map(...))`), and `Map.forEach((value, key) => ...)`.

Co-authored-by: axiomcode-bot[bot] <334110751+axiomcode-bot[bot]@users.noreply.github.com>
@swapnilpaliwal-sd
swapnilpaliwal-sd force-pushed the loop/ty-ts-contextual-lambda-param branch from 6f0089c to 860f0b6 Compare September 29, 2026 03:16
@swapnilpaliwal-sd
swapnilpaliwal-sd merged commit 12fa561 into 0.1.8 Sep 29, 2026
12 checks passed
@swapnilpaliwal-sd
swapnilpaliwal-sd deleted the loop/ty-ts-contextual-lambda-param branch September 29, 2026 03:49
@swapnilpaliwal-sd swapnilpaliwal-sd mentioned this pull request Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant