- API 路由统一: 移除 /api/h5/,统一 /api/pms/ 入口 - 二进制改名: api-server -> pms-api(与服务名一致) - nginx: 合并 /api/ + /pms/api/ 为 /api/pms/ - 新增 docs/API.md 接口文档 - 后端: CabinetDetail 增加 rssi/pow_fail/address/soc 等字段 - Redis 管道批量读取仓体实时数据(SOC/电压/电流/功率) - DB: 所有 TIMESTAMP 列改为 DATETIME(修复类型不匹配) - 用户管理: 新增创建时间/修改时间列,状态改用 Switch - 角色管理/用户管理: 操作按钮改为图标 - PC 设备详情: 基本信息补全 RSSI/ICCID/电源/地址 - PC 卡片: 通道统计改用 Lucide 图标,颜色统一 - PC 二维码: canvas 渲染,含编号文字,支持右键下载 - H5: 按 uni-app 规范修正导航(navigateTo 替代 reLaunch) - H5: 详情页加返回导航条 - H5: 扫码支持摄像头(jsQR)+ ?id= 参数 - H5: 底部 tabBar 在所有页面常显 - H5: 首页统计改为 6 项(设备/通道/空闲/充电/充满/故障) - H5: 下拉刷新 / 上拉加载 - 产品首页: 添加业务联系 - 文档: 清理过时文件,补充缺失规范章节
4.7 KiB
4.7 KiB
title, impact, impactDescription, type, tags
| title | impact | impactDescription | type | tags | |||||
|---|---|---|---|---|---|---|---|---|---|
| Do Not Use Generic Argument with reactive() | MEDIUM | The generic argument type differs from the actual return type due to ref unwrapping, causing type mismatches | gotcha |
|
Do Not Use Generic Argument with reactive()
Impact: MEDIUM - It is NOT recommended to use the generic argument of reactive() because the returned type, which handles nested ref unwrapping, is different from the generic argument type. Use interface annotation on the variable instead.
Task Checklist
- Use type annotation on the variable, not generic argument
- Understand that
reactive()unwraps nested refs - For generic composables, use
shallowRefor explicitRef<T>typing - Prefer
ref()for simple values to avoid these issues
The Problem
<script setup lang="ts">
import { reactive, ref } from 'vue'
interface Book {
title: string
year: number
author: Ref<string> // Nested ref
}
// WRONG: Generic argument doesn't account for ref unwrapping
const book = reactive<Book>({
title: 'Vue 3 Guide',
year: 2024,
author: ref('John Doe')
})
// TypeScript thinks book.author is Ref<string>
// But at runtime, it's unwrapped to just string!
book.author.value // TypeScript: OK, Runtime: ERROR (author is a string, not a ref)
</script>
The Solution: Interface Annotation
<script setup lang="ts">
import { reactive, ref } from 'vue'
interface Book {
title: string
year?: number
}
// CORRECT: Annotate the variable, not the generic
const book: Book = reactive({
title: 'Vue 3 Guide'
})
book.title = 'New Title' // TypeScript knows this is string
book.year = 2024 // TypeScript knows this is number | undefined
</script>
Why This Happens
When you use reactive(), Vue automatically unwraps any nested refs:
import { reactive, ref, Ref } from 'vue'
const name = ref('John')
const state = reactive({
name: name // This is a Ref<string>
})
// At runtime, state.name is 'John' (string), NOT a Ref
console.log(state.name) // 'John' (not ref object)
console.log(state.name.value) // Runtime error: .value doesn't exist
// The ACTUAL return type is different from what you'd expect
// reactive<{ name: Ref<string> }>() does NOT return { name: Ref<string> }
// It returns { name: string } due to automatic unwrapping
Correct Patterns
Pattern 1: Simple Interface Annotation
<script setup lang="ts">
interface FormState {
name: string
email: string
age: number
}
const form: FormState = reactive({
name: '',
email: '',
age: 0
})
</script>
Pattern 2: Partial for Optional Fields
<script setup lang="ts">
interface User {
id: string
name: string
email: string
}
// Start with partial data
const user: Partial<User> = reactive({})
// Fill in later
user.id = '123'
user.name = 'John'
</script>
Pattern 3: Use ref() Instead
For simpler cases, prefer ref() which has more predictable typing:
<script setup lang="ts">
interface User {
id: string
name: string
}
// ref() works well with generics
const user = ref<User>({
id: '1',
name: 'John'
})
// Access with .value - clear and predictable
user.value.name = 'Jane'
</script>
Generic Composables: Use Ref or shallowRef
When working with generic type parameters in composables:
// PROBLEM: Generic T with ref() causes UnwrapRef issues
function useBroken<T>(initial: T) {
const state = ref(initial) // Type becomes Ref<UnwrapRef<T>>
state.value = initial // Error: T is not assignable to UnwrapRef<T>
return state
}
// SOLUTION 1: Use explicit Ref<T> type
function useFixed1<T>(initial: T) {
const state: Ref<T> = ref(initial) as Ref<T>
return state
}
// SOLUTION 2: Use shallowRef (no unwrapping)
function useFixed2<T>(initial: T) {
const state = shallowRef(initial) // Properly typed as ShallowRef<T>
return state
}
When Generic Argument IS Safe
For simple non-ref values without nested reactivity, the generic is safe:
// Safe: no nested refs
const state = reactive<{ count: number; name: string }>({
count: 0,
name: ''
})
// Also safe: explicit simple types
const list = reactive<string[]>([])
const map = reactive<Map<string, number>>(new Map())
The issue only arises when:
- You have nested Ref types in your interface
- You're using generic type parameters that might contain refs