solidjs / solid-router

A universal router for Solid inspired by Ember and React Router
MIT License
1.13k stars 143 forks source link

`useRouteData` return wrong type #281

Closed denbon05 closed 7 months ago

denbon05 commented 1 year ago

Describe the bug

block.data.ts

export const BlockData: RouteDataFunc = ({
  params,
}: RouteDataFuncArgs): Resource<ETHBlock> => {
  const [block] = createResource(Number(params.id), fetchBlock);
  return block;
};

BlockMore.tsx

const BlockMore: Component = () => {
  const block = useRouteData<ETHBlock>();
  // const block = useRouteData<Resource<ETHBlock>>(); // doesn't work
  // const block = useRouteData() as unknown as Resource<ETHBlock>; // works
  return <div>{JSON.stringify(block(), null, 2)}</div>; // in fact it's callable
/*                             ^^^
This expression is not callable.
  Type 'Block' has no call signatures.ts
*/
};

export default BlockMore;

Your Example Website or App

https://github.com/denbon05/blockexplorer/blob/main/src/pages/BlockAbout.tsx

Steps to Reproduce the Bug or Issue

https://docs.solidjs.com/guides/how-to-guides/routing-in-solid/solid-router#data-functions Follow the documentation to face this bug.

Expected behavior

The router data function should infer the appropriate type.

Screenshots or Videos

Called block result in browser: image

Platform

Additional context

No response

ryansolid commented 1 year ago

It is supposed to be the type of the RouteData function which I admit in this case isn't very convenient but is more so in file system routed systems.

Ex.

import type { BlockData } from "./block.data.ts"

const BlockMore: Component = () => {
  const block = useRouteData<typeof BlockData>();
  return <div>{JSON.stringify(block(), null, 2)}</div>; // in fact it's callable
};

export default BlockMore;

Docs need to be clearer about this.

denbon05 commented 1 year ago

@ryansolid in that case I'm facing 'block()' is of type 'unknown' it seems we still have to use as like here:

const blockSrc = useRouteData<typeof BlockData>() as Resource<ETHBlock>;
ryansolid commented 7 months ago

This is related to the previous version of the router. TS issues with useRouteData was one of the motivations of changing it. Closing as addressing any bug here is no longer planned.