Skip to content

fix(vite): type generated refs by their picked exports - #20

Open
everton-dgn wants to merge 1 commit into
solidjs:mainfrom
everton-dgn:fix/route-types-pick
Open

everton-dgn wants to merge 1 commit into
solidjs:mainfrom
everton-dgn:fix/route-types-pick

Conversation

@everton-dgn

Copy link
Copy Markdown

With fileRoutes({ types: true }), every ref is typed as the full source module even though it selects only some exports. For a route like this:

export const route = { info: { title: "Home" } };
export default () => null;

The generated type rejects routes[0].$component.import = async () => ({ default: () => null }) because route is missing. It also accepts routes[0].$$route.require().default, which is absent at runtime.

Each ref is now typed as Pick<M, Extract<P, keyof M>>, where M is the source module and P comes from ref.pick without the synthetic $css name. This covers lazy and eager refs, including server pages. The $HEAD fallback picks GET. Picked names remain accessible as any when TypeScript has no types for a JavaScript route. Markdown modules keep generic refs; refs with no names left after filtering also use the generic type.

The tests compile generated declarations with small consumer files. Component mocks returning only default typecheck, and reads outside the pick fail in both manifest views. They also cover .js and .jsx routes without allowJs, with code splitting on and off. The new cases failed before their fixes and now pass. pnpm test and pnpm exec tsc --noEmit pass.

The only shared file with #17 is test/vite.spec.ts, and its diff applies cleanly on top. With #17, the server loads a server page's module whole while the client still picks default; the narrower type holds for both.

Type lazy and eager refs with Pick<M, Extract<P, keyof M>>, using the
names in ref.pick without the synthetic $css. Refs with no names left
use a generic type. Picked names stay accessible when the module is any.

Compile generated declarations with small consumer files to check that
mocks returning only default typecheck and reads outside the pick fail.
Cover untyped JavaScript routes with code splitting on and off.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant