ソースを参照

♻️ refactor(index): 优化广告类型定义

- 移除手动定义的Advertisement接口,使用hono的InferResponseType从API客户端自动生成类型
- 通过类型安全方式提取广告响应数据类型,提高代码可维护性和类型准确性
yourname 3 ヶ月 前
コミット
67c6e3e04b
1 ファイル変更5 行追加9 行削除
  1. 5 9
      mini/src/pages/index/index.tsx

+ 5 - 9
mini/src/pages/index/index.tsx

@@ -10,15 +10,11 @@ import { receiveCoupon } from '@/utils/coupon-api'
 import { Navbar } from '@/components/ui/navbar'
 import { TabBarLayout } from '@/layouts/tab-bar-layout'
 import { useAuth } from '@/utils/auth'
+import type { InferResponseType } from 'hono/client'
 
-interface Advertisement {
-  id: number
-  title: string
-  imageUrl: string
-  linkUrl: string
-  description: string
-  sortOrder: number
-}
+// 使用RPC类型安全提取广告响应类型
+type AdvertisementResponse = InferResponseType<typeof publicAdvertisementClient.$get, 200>
+type Advertisement = AdvertisementResponse['data'][0]
 
 interface WechatCouponStock {
   id: number
@@ -51,7 +47,7 @@ export default function IndexPage() {
       })
       if (response.status !== 200) throw new Error('获取广告失败')
       const result = await response.json()
-      return result.data as Advertisement[]
+      return result.data
     },
   })