Skip to content

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

Closed
@eric-crowell

Description

@eric-crowell

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions