我正在嘗試使用該Object.values(obj)
函式創建物件內所有鍵的值陣列,但它會引發以下錯誤:
No overload matches this call.
Overload 1 of 2, '(o: { [s: string]: string; } | ArrayLike<string>): string[]', gave the following error.
Argument of type 'SocialMedia | undefined' is not assignable to parameter of type '{ [s: string]: string; } | ArrayLike<string>'.
Type 'undefined' is not assignable to type '{ [s: string]: string; } | ArrayLike<string>'.
Overload 2 of 2, '(o: {}): any[]', gave the following error.
Argument of type 'SocialMedia | undefined' is not assignable to parameter of type '{}'.
Type 'undefined' is not assignable to type '{}'.ts(2769)
這是代碼的螢屏截圖,帶有物件的型別:

我嘗試使用顯式使用data.?socialMedia作為物件,但它沒有用。我嘗試了一個簡單的 for 回圈的解決方法,但這也不起作用。
uj5u.com熱心網友回復:
data?.socialMediaundefined如果是假的,會給出data。并Object.values()期望一個物件作為引數;這就是你得到的錯誤。您可以這樣做來修復它:
{data ? (
Object.values(data.socialMedia).map((social) => (
<div key={social} className="cursor-pointer">
{renderSocial(social)}
</div>
))
) : (
<></>
)}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/530707.html
上一篇:無法分配我的JSON以使用flatlist進行動態顯示
下一篇:列印*指標時出現記憶體錯誤
