reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.7k stars 1.17k forks source link

[RTK v2.0]: Function `createApi` Types are not Portable #3568

Closed eric-crowell closed 1 year ago

eric-crowell commented 1 year ago

For those experimenting with Redux Toolkit v2 (currently 2.0.0-Beta.0) and must export their own type declarations, there are quite a few issues with the portability of types from Redux Toolkit Query’s function createApi..

(There are also issues with functions like createSlice too, but I haven’t delved as deep into it, so I’m just focusing on createApi right now.)

Using createApi...

import { createApi } from '@reduxjs/toolkit/query';
export const myApi = createApi({ /** ... */ });

results in an error like so…

The inferred type of 'myApi' cannot be named without a reference to '../node_modules/@reduxjs/toolkit/dist/query/core/module.js'. This is likely not portable. A type annotation is necessary. ts(2742)

Significant TypeScript configurations:

{
   "moduleResolution": "Node16",
   "declaration": true,
}

Minimal Example with Error

https://codesandbox.io/p/sandbox/rtk-type-portability-j99lyh?file=%2Fsrc%2Frtk%2FmyApi.ts%3A1%2C1

Note: I believe this error does not occur for a majority of developers using RTK because it's normally included in applications that don’t need compiled declarations (declarations: false in tsconfig.json). This may be why this issue isn’t frequently brought to attention.

markerikson commented 1 year ago

Done in #3678 .

markerikson commented 2 months ago

Additional fixes in https://github.com/reduxjs/redux-toolkit/releases/tag/v2.2.7 !