You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const data = { t: 123, g: "rrr" }
function injectData(importedData: any) {
let k: keyof typeof data;
for (k in data) {
if (k in importedData)
data[k] = importedData[k]
}
}
errors
Type 'any' is not assignable to type 'never'.
這裡的 never 來自兩個 value 的類型的交集, 也就是 number & string
所以如果使用不同 value 類型的情況, 就放棄型別檢測吧XD
處理
const data = { t: 123, g: "rrr" }
function injectData(importedData: any) {
let k: keyof typeof data;
for (k in data) {
if (k in importedData)
// `(data as any)[k]` 也可過, 但意義不太一樣
(data[k] as any) = importedData[k];
}
}
tags: tstypescript
The text was updated successfully, but these errors were encountered:
延伸於 #80 的應用, 加入 assign 的行為就馬上進到另一個無解的死胡同... 😥
先說結論
還是用
any
就好...沒找到很優雅的解方
code
此情境是外部會給予任何可能的資料物件, 嘗試拿取屬性名稱交集的部分(進一步還需檢驗型別, 但和此議題無關)
errors
這裡的
never
來自兩個 value 的類型的交集, 也就是number & string
所以如果使用不同 value 類型的情況, 就放棄型別檢測吧XD
處理
tags:
ts
typescript
The text was updated successfully, but these errors were encountered: