Repository navigation
fix: type useRef<T>(null) and useRef<T>(undefined) like React 19 - #2281
Merged
Merged
Conversation
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
marked this pull request as ready for review
October 29, 2026 10:24
Deploying with
|
| 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 |
Deploying with
|
| 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 |
Contributor
Benchmark report🟢 No byte growth, no added work, and no timing changes beyond ±3%. Compares js-framework: 1k/10k row operationsAll 20 timed operations (20 without a verdict)
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)
|
This was referenced Oct 29, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
useRef<HTMLDivElement>(null)anduseRef<T>(undefined)now type-check. They return{ current: HTMLDivElement | null }and{ current: T | undefined }, matching React 19's@types/reactoverloads.useRef<T = undefined>()anduseRef<T>(initial: T, slot?).octane(runtime/hooks.ts),octane/server(runtime.server.ts), andoctane/universal(universal-core.ts). The compiler-appendedslotparameter 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 remainingoctane-tscerrors wereuseRef<T>(null)oruseRef<T>(undefined).docs/differences-from-react.mddocuments no divergence foruseReftyping. The gap was an omission, not a design choice.Changes
useRef<T>(initial: T | null, slot?)anduseRef<T>(initial: T | undefined, slot?)after the existinguseRef<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.packages/octane/typetests/use-ref-nullable-initial.test-d.ts, covering the client, server, and universal entries. It checks the nullablecurrenttype and rejects narrowing it away. It also coversundefinedinitializers, numeric and symbol slots, and checks that the unchanged inferred and mismatched-initializer cases behave as before.patchchangeset foroctane.Validation
octane-tsc -p packages/octane/typetests/tsconfig.jsononorigin/mainplus the new test reports 19 errors, all in the new file. With the fix it reports 0.runtime/hooks.tsgives 8 errors, onlyruntime.server.tsgives 6, and onlyuniversal-core.tsgives 5.pnpm typecheck:fileson the 3 source files and the typetest (packages/octane/tsconfig.json+packages/octane/typetests/tsconfig.json): pass.pnpm format:files:checkon 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.mjsfound 0 candidates. No hot path is touched, because overload signatures erase. esbuild output forruntime/hooks.ts,runtime.server.ts, anduniversal-core.tsis byte-identical before and after (sha256f01cac692ad9…,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 singleT | null | undefinedoverload (rejected: it would addundefinedtouseRef<T>(null)refs, unlike React); typing the overloads asRefObject<T>frompublic-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
(initial: T)overload comes first. Only calls that used to fail can now resolve. No@ts-expect-errorin the repo targets auseRef<T>(null | undefined)call.packages/aria/src/compat/react.tsre-exportsuseRefas<T>(initial: T | null) => { current: T }to work around this gap. Its return type dropsnull, 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 adapteruseRefis an implementer ABI, not an authored import, and is left unchanged.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith 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
useRefsouseRef<HTMLDivElement>(null)anduseRef<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, andoctane/universal. Existing overload order is preserved, souseRef(null),useRef<number>(1), and other calls that already worked are unchanged; compilerslotarguments still apply on the nullable overloads.A
use-ref-nullable-initial.test-d.tstypetest locks in the widenedcurrenttypes 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.