fix(dev): route arrow-function component exports typecheck correctly#15312
Open
ChihebBENCHEIKH1 wants to merge 1 commit into
Open
fix(dev): route arrow-function component exports typecheck correctly#15312ChihebBENCHEIKH1 wants to merge 1 commit into
ChihebBENCHEIKH1 wants to merge 1 commit into
Conversation
Emit `Route.ComponentProps` / `Route.ServerComponentProps` (and the
other `Route.*` annotation aliases) by indexing `GetAnnotations<...>`
directly instead of routing through an intermediate
`type Annotations = GetAnnotations<...>` alias. The intermediate alias
made TypeScript eagerly compute the full annotations object, tripping
its type-alias circularity detector (TS2456 / TS7022) when
`Route.ComponentProps` was used as the contextual parameter type of a
`const` arrow-function route component export (e.g.
`export const ServerComponent = ({}: Route.ComponentProps) => …`).
Fixes remix-run#14676
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.
Fixes #14676
Problem
Defining a route's
ServerComponent(or defaultComponent) as aconstarrow function whose parameter is typed withRoute.ComponentProps/Route.ServerComponentPropstriggers a TypeScript circular type-alias error:Named function declarations sidestep it, and adding an explicit variable type annotation is a workaround, but users reasonably expect the arrow form to Just Work.
Why it fails
Typegen emits each
+types/*.tswith an intermediate named alias:Naming
Annotationsas an object-type alias forces TypeScript to eagerly compute the full annotations object whenRoute.ComponentPropsis resolved. When that happens while inferring the type of aconstarrow initializer, the evaluation transitively reaches back intotypeof import("./self")and the alias-circularity detector fires.Function declarations skip it because TypeScript reads their signature without eagerly evaluating parameter annotations.
Fix
Drop the intermediate
Annotationsalias in the emitted file and index intoGetAnnotations<...>directly for everyRoute.*type:TypeScript resolves indexed access on a generic-instantiated type lazily per key, so only the requested property is instantiated and the cycle never forms. The types are semantically identical to what was emitted before.
Tests
Added two cases under
test.describe("server-first route component detection")→test.describe("arrow function route components")inintegration/typegen-test.ts:ServerComponent as arrow function typechecks(RSC framework mode,export const ServerComponent = (…: Route.ServerComponentProps) => …)default (Component) as arrow function typechecks(non-RSC,const Component = (…: Route.ComponentProps) => …; export default Component)Both assert
Expect<Equal<typeof loaderData, …>>/Expect<Equal<typeof actionData, …>>and runpnpm typecheck.