preactjs / preact-iso

Isomorphic utilities for Preact
MIT License
69 stars 9 forks source link

Router typings incompatible with class components #35

Closed foxt closed 2 months ago

foxt commented 2 months ago
<Router>
    {/* Type 'typeof ClassComponent' is not assignable to type 'AnyComponent<{ path: string; component: typeof ClassComponent; }> & (typeof ClassComponent | undefined)'. */}
    <Route path="/class" component={ClassComponent} />
    {/* works ok */}
    <Route path="/fc" component={FunctionComponent} />
</Router>

It works at runtime, but the typing has an error.

rschristian commented 2 months ago

I'm not entirely sure that's an issue here at the moment -- I thought AnyComponent (from preact itself) was meant to include class components.

Will need to double check.

Edit: have you explicitly typed that class component?

rschristian commented 2 months ago

Found the issue, but I think this should be corrected upstream. If you're willing to patch in the meantime (patch-package or pnpm patch are great for this sort of thing), you can do this:

 // node_modules/preact/src/index.d.ts
 export type AnyComponent<P = {}, S = {}> =
     | FunctionComponent<P>
-    | Component<P, S>;
+    | ComponentConstructor<P, S>;