Skip to content

fix(octane): infer useLinkedState's value when the reconciler declares previous - #1834

Merged
trueadm merged 2 commits into
mainfrom
fix/linked-state-return-inference
Oct 6, 2026
Merged

trueadm merged 2 commits into
mainfrom
fix/linked-state-return-inference

Conversation

@trueadm

@trueadm trueadm commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #1830. useLinkedState now infers its value from the reconciler's return type when the reconciler declares an unannotated previous parameter. The issue's example now types changed as boolean with no annotation.

Why

The bug wasn't specific to sourceEqual. Any reconciler that declared previous produced unknown unless something else named the value type, such as typed options, explicit type arguments, or an annotation. TypeScript gives an unannotated callback parameter its contextual type before it checks the body. Doing that fixes every type parameter the parameter's type names. previous: LinkedStatePrevious<Source, Value> named Value, so Value was fixed, to unknown, before the return type could infer it.

Changes

  • The declarations now take <Source, Value extends Previous, Previous = Value>:
    • Value is the reconciler's return type.
    • Previous is the type of previous.value (and of the options' valueEqual).
    • When TypeScript types previous, it fixes Previous instead of Value. Previous defaults to whatever Value has inferred so far, so Value stays free for the return type.
  • The tuple's value is unknown extends Previous ? Value : Previous. An annotated previous, typed options, or explicit type arguments still declare the state type, including one wider than the return type (for example an Item | null annotation with a reconciler that returns null). Without any of them, the tuple uses the return type.
  • Value extends Previous keeps every returned value storable as previous.value. A mismatched annotation or explicit type arguments are still errors.
  • The same shape applies to the octane, octane/server, and octane/universal declarations and their compiler-selected __useLinkedStateWithGetter variants. Implementation signatures and runtime code are unchanged.
  • docs/tsrx-basics.md and the website's core APIs page note that a reconciler that reads previous.value should declare its type, with a return annotation or an annotated previous.

What remains a TypeScript limit: with no declared type, previous.value is unknown inside the reconciler. TypeScript has to type the parameter before it can read the body's return type, and it doesn't iterate to a fixpoint. That was already the case before this change. The annotation workaround from the issue still works, and now also types previous.value.

Validation

  • pnpm format:check: not run locally (scoped pnpm format:files:check over all 7 changed files passes)
  • pnpm typecheck: not run in full locally. Scoped octane-tsc passes for packages/octane (+ typetests, typetests/react-compat), and for every project that calls useLinkedState: apollo-client (+ typetests), aria, lynx (+ typetests), radix, sanity-loader, tanstack-router, xstate, octane-evals, vite-plugin-octane, website. packages/select shows the same 3 components.tsrx Ref errors on main without this change (it is not in the root typecheck).
  • pnpm test: not run locally (type-only change; CI runs it)
  • targeted tests: new cases in packages/octane/typetests/hooks.test-d.ts:
    • the issue case on the client, server, and universal entry points, including a setter that rejects a non-boolean
    • a return annotation types previous.value
    • an annotated previous wider than the return keeps its type
    • a mismatched annotation and a mismatched explicit <Source, Value, Previous> are rejected
  • Mutation checks, each confirmed red and then restored:
    • original declarations → 4 errors on the issue cases (client, server, universal, setter)
    • tuple = return type only → the wide-annotation case fails
    • constraint removed → both mismatch @ts-expect-errors go unused

Risk / follow-ups

  • Type-only. TypeScript erases these declarations, so runtime, SSR, hydration, and bundle output are unchanged.
  • In generic wrappers that pass explicit generic arguments (for example useLinkedState<T, T>), the tuple shows as a deferred unknown extends T ? T : T in hovers. It is assignable to and from T, and every binding that does this still typechecks.
  • The one behavior change: with any as the previous.value type, the tuple takes the return type rather than any.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Low Risk
Type-only declaration and documentation changes with no runtime, SSR, or hydration impact; the main behavior shift is stricter or different inference at compile time.

Overview
Fixes a TypeScript inference bug where useLinkedState typed the linked value as unknown whenever the reconciler declared an unannotated previous parameter (contextual typing fixed the old single Value parameter before the return type could infer).

The public declarations now use &lt;Source, Value extends Previous, Previous = Value&gt;: Value is the reconciler’s return type, Previous is previous.value / valueEqual, and the tuple’s value type is Previous when known, otherwise Value. The same signature shape is applied on client, server, and universal entry points plus __useLinkedStateWithGetter. Runtime behavior is unchanged.

Docs note that reconcilers that read previous.value should still annotate the return type or previous. New hooks.test-d.ts cases lock in the issue fix, return-annotation behavior, wide previous annotations, and mismatch errors.

Reviewed by Cursor Bugbot for commit d332555. Bugbot is set up for automated code reviews on this repo. Configure here.

trueadm and others added 2 commits October 6, 2026 22:52
…s previous

Contextually typing an unannotated `previous` parameter fixed the linked
value type before TypeScript read the reconciler's return type, so the
tuple became `unknown` whenever nothing else named the value type (#1830).

Split the declarations into `Value` (the return type) and `Previous` (the
type of `previous.value`, defaulting to `Value`). The tuple uses `Previous`
when it is known from an annotated `previous`, typed options, or explicit
type arguments, and falls back to the returned type otherwise.
`Value extends Previous` keeps returns storable as `previous.value`. The
client, server, and universal entry points share the shape.

Closes #1830

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 6, 2026
@trueadm
trueadm marked this pull request as ready for review October 6, 2026 21:53
@vercel

vercel Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated
octane-website Skipped Skipped Oct 6, 2026 9:53pm UTC
octane-website-mcp Skipped Skipped Oct 6, 2026 9:53pm UTC

Request Review

@vercel
vercel Bot temporarily deployed to Preview – octane-website-mcp October 6, 2026 21:53 Inactive
@vercel
vercel Bot temporarily deployed to Preview – octane-website October 6, 2026 21:53 Inactive
@trueadm
trueadm merged commit 5722871 into main Oct 6, 2026
26 of 45 checks passed
@trueadm
trueadm deleted the fix/linked-state-return-inference branch October 6, 2026 22:02
trueadm added a commit that referenced this pull request Oct 6, 2026
Budget-only raise for #1833, which deferred its raise until
#1831 landed because both raise the same budgets. #1832 covered #1831 alone,
so main 50e18cc fails test shard 1/4 at "Verify every bundle budget"
(cli-spa-starter raw 101,793 over 100,464).

#1833 keeps link records for keyed-list rollback (record forms plus the link
notes in each relinking reconcile path). Its own measurement: +1,359 raw /
+443 gzip on rows. Measured on main 50e18cc with
`run-minimal.mjs --write-budgets` and `run.mjs --write-budgets octane-tsrx
octane-jsx` (measured + 32 raw/gzip, + 256 brotli); 60 raises, no lowerings:
- every client scenario and app: about +1,355 raw, +415 to +475 gzip
- cli-spa-starter 100,464 -> 101,825 raw, 33,454 -> 33,883 gzip
- root-static 163,723 -> 165,078 raw; rows framework 104,467 -> 105,826 raw
- jsx framework 177,637 -> 178,994 raw
- brotli rises where the measurement passed the old value (+256 headroom)

Bisected per commit on cli-spa-starter: 4dcb699 100,347 raw, #1833
101,708, #1834 101,708, #1831 101,793.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
trueadm added a commit that referenced this pull request Oct 6, 2026
…ate note

#1834 added a TypeScript paragraph to core-apis.mdx above the API tables, so
the four deferred scrollable-region-focusable tables each moved down one
child: .table-scroll:nth-child(114/138/143/203) -> (115/139/144/204). The
website integration suite's a11y sweep failed both themes on main 8b5b828
with exactly that shift; the findings themselves are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch was previously deployed

2 inactive deployments
Preview – octane-website — d3325552 Deployed Oct 6, 2026 by vercel[bot]
Preview – octane-website-mcp — d3325552 Deployed Oct 6, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useLinkedState infers unknown when derive reads previous with sourceEqual

1 participant