Getting TypeScript errors with invalidatesTags when referencing tags from other APIs that were added dynamically #5118
-
| Hi, I'm working on migrating my app to TypeScript and have run into an issue. My app uses code splitting and is divided up into many feature modules. Each feature module has an  Sometimes, an API in one module has to invalidate a tag from another module. But because these tags are dynamically registered with  Here is a simplified and bit of a contrived example: users/api.ts Then, in another file: user-edit/api.ts I get this error on the invalidatesTags: I think the  Either way, TypeScript is mad at me because it doesn't see the imported tag as a valid tag value to use here. Is there a way to make TypeScript happy here, without having to statically specify all of my tags from all of my modules (some of which may not be loaded for a given user) in the API slice definition? Thanks! | 
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
| @phryneas would be able to answer better here, but I'm pretty sure this is just an inherent limitation of static typing. A given API instance can only know about the tags defined within itself.  So, if you have  Given that, the options are really either do some casting here to make TS happy and accept that you're technically bypassing type safety in this small case, or define all tags at the root API level. | 
Beta Was this translation helpful? Give feedback.
@phryneas would be able to answer better here, but I'm pretty sure this is just an inherent limitation of static typing.
A given API instance can only know about the tags defined within itself. So, if you have
baseApi,api1 = baseApi.injectEndpoints(), andapi2 = baseApi.injectEndpoints(),api1knows about the tags frombaseApiand itself, but has no way to know tags that are defined inapi2. They're entirely separate objects with no type-level relation to each other. Static and dynamic behavior end up at odds with each other.Given that, the options are really either do some casting here to make TS happy and accept that you're technically bypassing type safety in this small case, or defi…