Skip to content

Virtual TSX drops the let __spread_props declaration for a host element with ref plus a spread outside a direct return, producing TS2304 #737

Closed

Description

@dudusotero

Summary

When a host element carries both a ref attribute and a JSX spread,
octane/compiler/volar lowers the spread to a generated binding:

let A__spread_props1 = __normalize_spread_props_for_ref_attr(rest);
<text {...A__spread_props1} ref={[r, A__spread_props1.ref]} />

The let is only actually emitted when the element is the operand of a direct
return statement, or when it sits inside a native template directive
(@if/@for), which wraps it in an IIFE. In every other position, the
declaration is dropped while the references to it survive, so the virtual TSX
contains an undefined name and TypeScript reports TS2304.

The runtime compile path (octane/compiler, compile.js) produces correct
output for every position tested. The defect is confined to the type-only
virtual TSX, so the symptom is editor diagnostics and tsrx-tsc failures on
source that builds and runs correctly.

Found while working #721.

Environment

Minimal reproduction

import { compileToVolarMappings } from 'octane/compiler/volar';

const broken = `export function A(props: any) {
	const { c, r, ...rest } = props;
	return <svg>{c ? <text ref={r} {...rest} /> : null}</svg>;
}`;

const working = `export function B(props: any) {
	const { r, ...rest } = props;
	return <text ref={r} {...rest} />;
}`;

console.log(compileToVolarMappings(broken, 'probe.tsrx').code);
console.log(compileToVolarMappings(working, 'probe.tsrx').code);

Actual result

Expression position, declaration missing:

/** @jsxImportSource octane */
import {
	normalize_spread_props_for_ref_attr as __normalize_spread_props_for_ref_attr
} from 'octane/tsrx-spread';

export function A(props: any) {
	const { c, r, ...rest } = props;

	return <svg>{c
			? <text {...A__spread_props1} ref={[r, A__spread_props1.ref]} />
			: null}</svg>;
}

A__spread_props1 is never declared, and the imported helper is never called.
compileToVolarMappings reports no errors. TypeScript reports TS2304 "Cannot
find name 'A__spread_props1'" once per reference.

Direct return statement, declaration present:

export function B(props: any) {
	const { r, ...rest } = props;

	{
		let B__spread_props1 = __normalize_spread_props_for_ref_attr(rest);

		return <text {...B__spread_props1} ref={[r, B__spread_props1.ref]} />;
	}
}

Three positions additionally lower the element twice, emitting two undeclared
names and a nested ref array:

compileToVolarMappings('export const A=(p:any)=><text ref={p.r} {...p.rest} />;', 'probe.tsrx').code;
export const A = (p: any) => <text {...A__spread_props2} ref={[A__spread_props2.ref, [p.r, A__spread_props1.ref]]} />;

Expected result

Every position emits the declaration. The mechanism already exists:
wrap_jsx_setup_declarations in @tsrx/core/src/transform/jsx/index.js:6275
wraps the expression in (() => { let …; return …; })(), and the native @if
path uses it:

return <svg>{c
		? (() => {
			let A__spread_props1 = __normalize_spread_props_for_ref_attr(rest);

			return <text {...A__spread_props1} ref={[r, A__spread_props1.ref]} />;
		})()
		: null}</svg>;

The double-lowering must also be lowered exactly once per element.

Position matrix

Every case uses the same element, <text ref={r} {...rest} />. "Runtime" is
compile() from octane/compiler.

# Position Virtual TSX Runtime
1 return <text … /> ok ok
2 return <svg><text … /></svg> ok ok
3 const e = <text … /> broken, 2 undeclared names ok
4 return c ? <text … /> : null broken, 2 undeclared names ok
5 return <svg>{c ? <text … /> : null}</svg> broken, 1 undeclared name ok
6 return <svg>{c && <text … />}</svg> broken, 1 undeclared name ok
7 export const A = (p: any) => <text … /> broken, 2 undeclared names ok
8 xs.map((x: any) => <text … />) broken, 1 undeclared name ok
9 @{ <svg>@if (c) { <text … /> }</svg> } ok ok

Only a direct return statement and the native template directive work.

Root cause

The lowering attaches the declaration to the element's metadata via
add_jsx_setup_declaration (@tsrx/core/src/transform/jsx/index.js:6199), then
relies on a later pass to hoist it. There are exactly two consumers:

  • extract_jsx_setup_declarations at line 1380, in the render-block statement
    builder, covering cases 1, 2 and 9.
  • wrap_jsx_setup_declarations at line 701, reached only from the
    TSRXJSXFragment/TSRXJSXElement visitor, covering the native template
    directive path.

Plain-JS expression positions (conditional expression, logical expression,
concise arrow body, variable declarator init, callback body) reach neither, so
the metadata is discarded with the node while the rewritten attributes, which
already reference the generated identifier, are printed.

Blast radius

Scanned all 881 .tsrx files under packages/*/src through
compileToVolarMappings, counting __spread_props identifiers referenced
without a declaration.

Package Undeclared references
visx 4
all others 0
  • packages/visx/src/tooltip/tooltips/Tooltip.tsrx (concise arrow body, case 7): Tooltip__spread_props1, Tooltip__spread_props2
  • packages/visx/src/geo/projections/Projection.tsrx (.map callback, case 8): Projection__spread_props1
  • packages/visx/src/text/Text.tsrx (ternary inside a JSX hole, case 5): Text__spread_props1

These account for all 7 TS2304 in
tsrx-tsc --noEmit -p packages/visx/tsconfig.json.

The pattern itself is common. The same scan counts 135 host ref plus spread
lowerings that do emit their declaration, across 16 packages:

drei 75, visx 23, recharts 9, cmdk 8, pdf 3, lexical 2, day-picker 2,
colorful 2, remix-router 2, spring 2, tanstack-start 2, input-otp 1,
tiptap 1, three 1, vaul 1, textarea-autosize 1

Those 135 are all in supported positions. The defect is narrow today because
bindings overwhelmingly write the element as a direct return, not because the
broken positions are unusual authoring.

Ownership assessment

Octane's compiler, not @octanejs/visx. All three authored sites are ordinary
.tsrx: nothing in docs/differences-from-react.md or
.rulesync/rules/tsrx-authoring.md restricts where a host element with ref
plus a spread may appear, and the authoring rule documents ref as a plain prop
with no positional constraint. The @if path proving the IIFE wrapper already
exists shows expression position is intended to be supported rather than a
deliberate limitation. The runtime compiles all nine positions correctly, so the
divergence is inside the type-only print reached through
octane/compiler/volar.

Rewriting the three visx components to hoist the element into a return would
hide a compiler defect behind app changes and leave the position broken for
every consumer.

Activity

  1. trueadm commented on Aug 14, 2026

    @trueadm
    Collaborator

    Is this fixed now?

  2. dudusotero commented on Aug 14, 2026

    @dudusotero
    ContributorAuthor

    Is this fixed now?

    Not fixed yet.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions