I would like to wrap the following function in mobx's observer()
, but can't seem to figure out how to do that (or if it's possible) while maintaining the generic typing:
interface GenericProps<T> {
arg: T;
}
const foo: <T>(props: GenericProps<T>) => T = (props) => props.arg;
This is a global exported function, so using the @observer
decorator is not an option (I think), I need to use the observer()
function from mobx-lite-react.
What I want is something like
interface GenericProps<T> {
arg: T;
}
const foo = observer<T>((props: GenericProps<T>) => props.arg);
Probably related to https://github.com/mobxjs/mobx-react-lite/issues/243, but from the conversation it is not clear to me how I would go about implementing the generic observer in my case.
React.FunctionComponent
interfaceconst foo_1: <T>(props: GenericProps<T>) => React.ReactElement | null = (props) => <>{JSON.stringify(props.arg)}</>;
const foo_2: <T>(props: GenericProps<T>) => React.ReactElement | null = observer((props) => <>{JSON.stringify(props.arg)}</>);
extends unknown
which tells compiler this is generic definition and not JSX tag, return type doesn't need to be specified since it will be inferred)const foo_3 = observer(<T extends unknown>(props: GenericProps<T>) => <>{JSON.stringify(props.arg)}</>);