如何在redux-toolkit中键入providEtags(许多项目端点)

发布于 2025-02-05 14:20:18 字数 2633 浏览 2 评论 0原文

以下代码运行良好,但是我不设法键入providestags正确:

type Post = {
  id: number,
  name: string,
  description: string,
}

const postsApiSlice = api.injectEndpoints({
    endpoints: (builder) => ({
      getPosts: builder.query<EntityState<Post>, void>({
        query: ROUTES.POSTS,
        transformResponse: (responseData: Post[]) => {
          return adapter.setAll(initialState, responseData)
        },
        // Typescript error is here, at provideTags
        providesTags: (result) => {
          // What to do if result is undefined?
          if (!result) return [{ type: POST_TAG, id: 'LIST' }]
  
          const tags = (result.ids.length > 0) ?
            // Typescript accepts type of next line if I return it
            result.ids.map((id) => ({ type: POST_TAG, id }))
            :
            // Typescript also accepts type of next line if I return it
            [{ type: POST_TAG, id: 'LIST' }]
          return tags
        }
      }),
    }),
  })

typescript错误我得到:

Type '(result: EntityState<Post> | undefined) => { type: string; id: EntityId; }[]' is not assignable to type 'ResultDescription<"Post", EntityState<Post>, void, FetchBaseQueryError, {} | undefined> | undefined'.
  Type '(result: EntityState<Post> | undefined) => { type: string; id: EntityId; }[]' is not assignable to type 'GetResultDescriptionFn<"Post", EntityState<Post>, void, FetchBaseQueryError, {} | undefined>'.
    Type '{ type: string; id: EntityId; }[]' is not assignable to type 'readonly TagDescription<"Post">[]'.
      Type '{ type: string; id: EntityId; }' is not assignable to type 'TagDescription<"Post">'.
        Type '{ type: string; id: EntityId; }' is not assignable to type 'FullTagDescription<"Post">'.
          Types of property 'type' are incompatible.
            Type 'string' is not assignable to type '"Post"'.ts(2322)
endpointDefinitions.d.ts(188, 5): The expected type comes from property 'providesTags' which is declared here on type 'Omit<EndpointDefinitionWithQuery<void, BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError, {}, {}>, EntityState<Post>> & { ...; } & { ...; } & QueryExtraOptions<...>, "type"> | Omit<...>'

如果我返回result.ids.map((id)=&gt;({type:post_tag) ,id})))或仅[{type:post_tag,id:'list'}]正常工作。

  • 我该如何输入?
  • 另外,不确定结果是未定义时该怎么办。我应该返回[{type:post_tag,id:'list'}]

Following code works well, however I dont manage to type providesTags correctly:

type Post = {
  id: number,
  name: string,
  description: string,
}

const postsApiSlice = api.injectEndpoints({
    endpoints: (builder) => ({
      getPosts: builder.query<EntityState<Post>, void>({
        query: ROUTES.POSTS,
        transformResponse: (responseData: Post[]) => {
          return adapter.setAll(initialState, responseData)
        },
        // Typescript error is here, at provideTags
        providesTags: (result) => {
          // What to do if result is undefined?
          if (!result) return [{ type: POST_TAG, id: 'LIST' }]
  
          const tags = (result.ids.length > 0) ?
            // Typescript accepts type of next line if I return it
            result.ids.map((id) => ({ type: POST_TAG, id }))
            :
            // Typescript also accepts type of next line if I return it
            [{ type: POST_TAG, id: 'LIST' }]
          return tags
        }
      }),
    }),
  })

Typescript error I get:

Type '(result: EntityState<Post> | undefined) => { type: string; id: EntityId; }[]' is not assignable to type 'ResultDescription<"Post", EntityState<Post>, void, FetchBaseQueryError, {} | undefined> | undefined'.
  Type '(result: EntityState<Post> | undefined) => { type: string; id: EntityId; }[]' is not assignable to type 'GetResultDescriptionFn<"Post", EntityState<Post>, void, FetchBaseQueryError, {} | undefined>'.
    Type '{ type: string; id: EntityId; }[]' is not assignable to type 'readonly TagDescription<"Post">[]'.
      Type '{ type: string; id: EntityId; }' is not assignable to type 'TagDescription<"Post">'.
        Type '{ type: string; id: EntityId; }' is not assignable to type 'FullTagDescription<"Post">'.
          Types of property 'type' are incompatible.
            Type 'string' is not assignable to type '"Post"'.ts(2322)
endpointDefinitions.d.ts(188, 5): The expected type comes from property 'providesTags' which is declared here on type 'Omit<EndpointDefinitionWithQuery<void, BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError, {}, {}>, EntityState<Post>> & { ...; } & { ...; } & QueryExtraOptions<...>, "type"> | Omit<...>'

If I return just result.ids.map((id) => ({ type: POST_TAG, id })) or just [{ type: POST_TAG, id: 'LIST' }] works correctly.

  • How can I type it?
  • Also, not sure what should I do when result is undefined. Should I return [{ type: POST_TAG, id: 'LIST' }]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白况 2025-02-12 14:20:19

这在

当您定义post_tag时,您将需要一些作为const断言。

因此,不是const post_tag =“ foo”,而是const post_tag =“ foo”,因为const应该可以解决问题。否则,它将被键入“字符串”,而不是“ foo”。

This is described in typing providesTag/invalidatesTags.

You will need a few as const assertions when you define POST_TAG.

So not const POST_TAG = "foo", but const POST_TAG = "foo" as const should do the trick. Otherwise it will be typed as "string", not as "foo".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文