preactjs/preact

Compat does not export all members that @types/react does

Opened this issue · 7 comments

Reproduction

I'm currently attempting to use MobX (specificially, mobx-react-lite) in a Preact app, and I'm running into trouble with some of the interfaces it requires.

Versions

├── eslint-config-preact@1.1.0
├── preact@10.3.4
├── preact-cli@3.0.0-rc.10
├── preact-feather@4.1.0
├── preact-render-to-string@5.1.4
├── preact-router@3.2.1

Steps to reproduce

Wrapping a MobX observer around a functional component will cause this issue:

import { FunctionalComponent, h } from 'preact';
import { observer as mobxObserver } from "mobx-react-lite";

function observer<P>(props: P) {
  return mobxObserver(props as any);
}

const ExampleComponent: FunctionalComponent = observer(() => { 
... 
});

Expected Behavior

The app should be able to be built.

Actual Behavior

I received this error after trying to do a production build of the app (note: it does work as expected in develop):

✖ ERROR ./node_modules/mobx-react-lite/dist/observer.d.ts
ERROR in ./node_modules/mobx-react-lite/dist/observer.d.ts(5,84):
TS2694: Namespace 'React' has no exported member 'RefForwardingComponent'.

After opening up node_modules/mobx-react-lite/dist/observer.d.ts, this is what I found:

export declare function observer<P extends object, TRef = {}>(baseComponent: React.RefForwardingComponent<TRef, P>, options: IObserverOptions & {
    forwardRef: true;
}): React.MemoExoticComponent<React.ForwardRefExoticComponent<React.PropsWithoutRef<P> & React.RefAttributes<TRef>>>;

There are errors on RefForwardingComponent, MemoExoticComponent, PropsWithoutRef, and RefAttributes, as none of these items are exported from compat's React namespace.

My question is how do I solve this, or is this something I can even solve on my own end?

This might be related to #1930 and #2329

I have the same problem after mobx-react 6.1.8.

We've had a couple of issues about this, so I'll close this one for now

Here's a possible workaround
#2150 (comment)

If you could try out the changes in #2329 and report there if that fixes it that would also help

Neither of those actually export even 1 out of the 4 interfaces needed, so they both fail, while the first solution starts to break other libraries too.

I don't quite see how this can be closed when no solution is offered that works.

cause it's very similar to existing issues, it's not closed because it's solved

i.e. #2222

Workaround here: #2150 (comment). You may need to customize it. I agree it's not a great situation, so please upvote the issues, both this one and #2222 and #2150.