Skip to content

Inferred type from createAsyncThunk causing TypeScript error: "The inferred type of ... cannot be named without a reference to ..." #472

@sammyers

Description

@sammyers

Trying to use createAsyncThunk in TypeScript, with a pretty basic use case:

export const fetchStationSpecs = createAsyncThunk("stations/fetchStationSpecs", async () => {
  const response = await new TestingDaemonApiRequest("station_specs").doFetch(true);
  const stations = (await response.json()).data as IStationCommandMap;
  return stations;
});

tsc throws this error:

The inferred type of 'fetchStationSpecs' cannot be named without a reference to '../../../../../../../build/js/rush_temp/node_modules/.pnpm/nexus.skyd.io/redux-thunk/2.3.0/node_modules/redux-thunk'. This is likely not portable. A type annotation is necessary.

This looks to be due to some trickiness with transitive dependencies as described here - the inferred return type from createAsyncThunk relies on the ThunkDispatch type from redux-thunk, which is unavailable to my package since it's a transitive dependency (this may not be an issue for other build setups, but appears to be a common class of problem for monorepos using pnpm or similar systems).

Fortunately, this problem completely goes away if ThunkDispatch is exported directly from Redux Toolkit (thus making it available to the package that depends on it), which I think would be useful to developers for making thunk-related utilities anyway. Going to make the shortest pull request ever to fix this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions