cerebral/overmind

[BUG] type inference all actions as unknown (documenting)

amrdraz opened this issue · 4 comments

I'm posting this issue for documentation purpose in case someone faces a similar issue, it should probably get closed immediately.

I have config that looks like this

import {
  json,
  derived,
  IConfig,
  IAction,
  IOperator,
  IOnInitialize,
  IContext,
} from 'overmind';
import { merge } from 'overmind/config';
import {
  createHook,
  createStateHook,
  createActionsHook,
  createEffectsHook,
  createReactionHook,
} from 'overmind-react';
import { state } from './state';
import * as actions from './actions';
import * as effects from './effects';
import { onInitialize } from './initialize';
import namespaces from './namespaces';

export const config = merge(
  { onInitialize, state, effects, actions },
  namespaces,
);


export const useOvermind = createHook<typeof config>();
export const useStates = createStateHook<typeof config>();
export const useActions = createActionsHook<typeof config>();
export const useEffects = createEffectsHook<typeof config>();
export const useReaction = createReactionHook<typeof config>();

I am in a react native project and was on v23.1.0 when I upgraded to v24.1.1

While I am developing all my actions got the type unknown

state and effects where inferred normally

but when I hover over the useActions it shows a types of the form

const useActions: () => {
    readonly resetGlobalError: unknown;
    readonly setGlobalError: unknown;
    readonly rehydrateState: unknown;
    settings: {
        readonly saveSettings: unknown;
        ... 5 more ...;
        readonly resetAllUserFlagValues: unknown;
    };
    ... 5 more ...;
}

instead of

const useActions: () => {
    readonly resetGlobalError: () => void;
    readonly setGlobalError: (value: string) => void;
    readonly rehydrateState: (value: any[]) => void;
    settings: {
        readonly saveSettings: () => Promise<void>;
        ... 5 more ...;
        readonly resetAllUserFlagValues: (value: string) => void;
    };
    ... 5 more ...;
}

I should add that I did not have this problem with a web project where I am using v24.1.1

So I'm kind of stumped

I revered back to v23 and everything worked out well
then I switched forward again to 24 and then everything worked

I'm not sure what I did that triggered this, but it restarting the VS code TS server wasn't enough to make it go away only after switching versions and re installing the new one did it work 🤷

just wanted to document this somewhat weird phenomena

So for reference this issue still occurs with v25.x
and my little obscure hack of uninstalling and installing or refreshing doesn't seem to fix it

we do have some devs that don't have the problem (one using ubunto not OSX like the rest of us)
but also some didn't have the problem and then it magically occurred

I observed that when the issue is happening with all actions that are defined as function (not typed to AsyncAction or Action) do show up normally so maybe that's a hint to something

ok the the issue has to do somehow with circular type reference I think.

I figured this out after trying to use the declared as opposed to explicit approach of type declaration as per the documentation

Initially we had explicit typings, but when we started to switch to declared, we noticed that all the actions are showing up again... at least as functions.

When I went to one of the namespaces and had it reference overmind's AsyncAction and Action instead of our explicit type
the namespace actions became unknown

the only reason the others were still showing is because I had removed the explicit declaration of Config and so messed up the other explicit types rendering all the actions as normal functions any for state.

ok so it got fixed for now by upgrading to "overmind-react": "^26.0.1"

Great! 😄 Working with 27 as well?