Skip to content

fix: type useRef<T>(null) and useRef<T>(undefined) like React 19 - #2281

Merged
trueadm merged 1 commit into
mainfrom
fix/useref-null-overloads
Oct 29, 2026
Merged

trueadm merged 1 commit into
mainfrom
fix/useref-null-overloads

Conversation

@trueadm

@trueadm trueadm commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • useRef<HTMLDivElement>(null) and useRef<T>(undefined) now type-check. They return { current: HTMLDivElement | null } and { current: T | undefined }, matching React 19's @types/react overloads.
  • Before this change, both reported TS2345 ("Argument of type 'null' is not assignable to parameter of type 'HTMLDivElement'"), because Octane declared only useRef<T = undefined>() and useRef<T>(initial: T, slot?).
  • This change adds only types. All three declarations that consumers import get the overloads: octane (runtime/hooks.ts), octane/server (runtime.server.ts), and octane/universal (universal-core.ts). The compiler-appended slot parameter still works on every overload.

Why

  • useRef<HTMLElement>(null) is React's documented ref pattern, so every migrated React 19 app hits this error. In the Memos app migrated with @octanejs/codemod (fix(codemod): fix what migrating Memos and T3 Code found #2280), 31 of the 68 remaining octane-tsc errors were useRef<T>(null) or useRef<T>(undefined).
  • docs/differences-from-react.md documents no divergence for useRef typing. The gap was an omission, not a design choice.

Changes

  • Adds useRef<T>(initial: T | null, slot?) and useRef<T>(initial: T | undefined, slot?) after the existing useRef<T>(initial: T, slot?) overload, in the same order as React. Inferred calls (useRef(null) gives { current: null }) and explicit non-nullable calls (useRef<number>(1)) still resolve to the first matching overload, so their types do not change.
  • Adds a typetest, packages/octane/typetests/use-ref-nullable-initial.test-d.ts, covering the client, server, and universal entries. It checks the nullable current type and rejects narrowing it away. It also covers undefined initializers, numeric and symbol slots, and checks that the unchanged inferred and mismatched-initializer cases behave as before.
  • Adds a patch changeset for octane.

Validation

  • Regression typetest fails before the fix: octane-tsc -p packages/octane/typetests/tsconfig.json on origin/main plus the new test reports 19 errors, all in the new file. With the fix it reports 0.
  • Each declaration has its own guard. Reverting only runtime/hooks.ts gives 8 errors, only runtime.server.ts gives 6, and only universal-core.ts gives 5.
  • pnpm typecheck:files on the 3 source files and the typetest (packages/octane/tsconfig.json + packages/octane/typetests/tsconfig.json): pass.
  • pnpm format:files:check on every changed file: pass.
  • pnpm sync: no generated changes.
  • pnpm format:check, pnpm typecheck, pnpm test: not run locally, per the local load rule. CI runs them.
  • perf-review: node scripts/perf-review-scan.mjs found 0 candidates. No hot path is touched, because overload signatures erase. esbuild output for runtime/hooks.ts, runtime.server.ts, and universal-core.ts is byte-identical before and after (sha256 f01cac692ad9…, 6620e5c866cb…, 1273e4cd3c63…).
  • concise-code: src +13 −0 (runtime +13 −0, all type-only overload lines, no emitted code). New file: the typetest, the regression coverage. New exports: none. Alternatives: a single T | null | undefined overload (rejected: it would add undefined to useRef<T>(null) refs, unlike React); typing the overloads as RefObject<T> from public-types (rejected: these files return structural { current }, and importing public types into the runtime would add a dependency for no benefit). Bytes: not applicable, because the emit is unchanged.

Risk / follow-ups

  • Overload resolution is unchanged for every call that type-checked before, because the existing (initial: T) overload comes first. Only calls that used to fail can now resolve. No @ts-expect-error in the repo targets a useRef<T>(null | undefined) call.
  • Follow-up: packages/aria/src/compat/react.ts re-exports useRef as <T>(initial: T | null) => { current: T } to work around this gap. Its return type drops null, which differs from React, so removing it needs an audit of aria's port. That is outside this PR.
  • packages/octane/src/compiler/valdi.d.ts's adapter useRef is an implementer ABI, not an authored import, and is left unchanged.

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.


Note

Low Risk
Type-only overload additions with unchanged emitted JS; overload resolution for previously valid calls is unchanged.

Overview
Adds React 19–aligned TypeScript overloads for useRef so useRef<HTMLDivElement>(null) and useRef<T>(undefined) type-check and return { current: T | null } / { current: T | undefined } instead of TS2345 on the initializer.

The new signatures are declared only (no runtime change) on all three public entry points: octane, octane/server, and octane/universal. Existing overload order is preserved, so useRef(null), useRef<number>(1), and other calls that already worked are unchanged; compiler slot arguments still apply on the nullable overloads.

A use-ref-nullable-initial.test-d.ts typetest locks in the widened current types and rejects invalid narrowing or mismatched initializers. A patch changeset documents the fix for consumers.

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

React 19's @types/react widens `current` when an explicit `T` gets a
`null` or `undefined` initializer, so `useRef<HTMLDivElement>(null)` is
`{ current: HTMLDivElement | null }`. Octane declared only `useRef<T>()`
and `useRef<T>(initial: T)`, so the common ref pattern failed with
TS2345. Add both overloads to the client, server, and universal
declarations, after the existing `(initial: T)` overload so every call
that already type-checked resolves the same way. Types only: the emit
is byte-identical.

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

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
octane-website-mcp 37c7bbe Commit Preview URL

Branch Preview URL
Oct 29 2026, 10:26 AM

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
octane-website 37c7bbe Commit Preview URL

Branch Preview URL
Oct 29 2026, 10:27 AM

@github-actions

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No byte growth, no added work, and no timing changes beyond ±3%.

Compares 29d1117db, the merge commit's first parent with 2e4292fb9 on the same runner. Any increase in a js-framework work counter fails this check; byte changes are a report. Wall time is a report: base and head samples alternate in one browser, and an operation is called slower or faster only when the 95% confidence interval of its ratio excludes ±3%. Workflow run

js-framework: 1k/10k row operations

All 20 timed operations (20 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 2.783 3.000 ±0.0% −3.7% … +7.3% ⚪ within noise
octane-tsrx replace 6.550 6.317 −0.0% −6.9% … +9.5% ⚪ within noise
octane-tsrx add 2.408 2.283 ±0.0% ±0.0% … +4.8% ⚪ within noise
octane-tsrx update 0.079 0.080 −2.2% −8.9% … +3.1% ⚪ within noise
octane-tsrx select 0.005 0.006 +1.8% −1.1% … +5.0% ⚪ within noise
octane-tsrx swap 0.018 0.018 +0.5% −4.7% … +2.7% ⚪ within noise
octane-tsrx remove 0.016 0.017 +1.6% −6.2% … +8.9% ⚪ within noise
octane-tsrx runlots 23.150 22.725 −3.5% −6.9% … +0.9% ⚪ within noise
octane-tsrx select_lots 0.006 0.006 +1.1% −5.0% … +7.2% ⚪ within noise
octane-tsrx clear 51.450 52.850 −0.8% −3.2% … +3.3% ⚪ within noise
octane-jsx run 3.850 3.925 ±0.0% −2.9% … +5.6% ⚪ within noise
octane-jsx replace 8.142 7.992 +3.1% −3.0% … +7.4% ⚪ within noise
octane-jsx add 3.500 3.525 +6.2% −4.1% … +10.0% ⚪ within noise
octane-jsx update 0.173 0.174 +0.6% −3.7% … +6.5% ⚪ within noise
octane-jsx select 0.084 0.081 −0.8% −3.3% … +1.2% ⚪ within noise
octane-jsx swap 0.134 0.137 +0.6% −2.0% … +6.7% ⚪ within noise
octane-jsx remove 0.121 0.118 +0.9% −2.5% … +4.6% ⚪ within noise
octane-jsx runlots 26.558 29.717 −2.6% −11.0% … +8.0% ⚪ within noise
octane-jsx select_lots 0.964 0.946 +0.4% −3.3% … +5.5% ⚪ within noise
octane-jsx clear 59.000 65.858 +9.2% −7.4% … +17.1% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

No changes across 111 measured values.

Public-import reachability bundles (bytes)

No changes across 141 measured values.

Lynx dual-thread render (report only)

All 13 timed operations (13 without a verdict)
target operation base ms head ms head/base 95% CI
octane-lynx empty_startup_ms 0.562 0.442 −21.3% – ⚪ unpaired
octane-lynx create_1k_rows_ms 11.271 8.060 −28.5% – ⚪ unpaired
octane-lynx create_10k_rows_ms 115.019 115.589 +0.5% – ⚪ unpaired
octane-lynx update_1k_rows_ms 2.762 2.036 −26.3% – ⚪ unpaired
octane-lynx update_10k_rows_ms 16.146 11.669 −27.7% – ⚪ unpaired
octane-lynx-reentrant-10k drain_ms 141.696 110.413 −22.1% – ⚪ unpaired
octane-lynx-reentrant-20k drain_ms 290.984 207.720 −28.6% – ⚪ unpaired
octane-lynx-cold create_1k_rows_ms 26.184 20.076 −23.3% – ⚪ unpaired
octane-lynx-cold create_10k_rows_ms 118.178 132.300 +12.0% – ⚪ unpaired
octane-lynx-first-screen create_1k_rows_ms 133.602 135.312 +1.3% – ⚪ unpaired
octane-lynx-first-screen create_10k_rows_ms 954.498 853.779 −10.6% – ⚪ unpaired
octane-lynx-adopt create_1k_rows_ms 161.258 151.052 −6.3% – ⚪ unpaired
octane-lynx-adopt create_10k_rows_ms 1628.310 1366.683 −16.1% – ⚪ unpaired

@trueadm
trueadm merged commit f0c0d36 into main Oct 29, 2026
55 checks passed
@trueadm
trueadm deleted the fix/useref-null-overloads branch October 29, 2026 10:45
trueadm added a commit that referenced this pull request Oct 29, 2026
octane now declares React 19's `useRef<T>(initial: T | null)` and
`useRef<T>(initial: T | undefined)` overloads (#2281), so the compat
layer no longer needs its own `useRef`. The cast it used typed
`useRef<Element>(null)` as `{ current: Element }`, dropping the `null`
that React and the upstream React Aria source rely on. `export * from
'octane'` now supplies `useRef`, with the same runtime function.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant