使用以下代碼行創建條件類時:
className={({ selected }) =>
classNames(
selected
? 'bg-gray-100 text-gray-900'
: 'text-gray-600 hover:bg-gray-50 hover:text-gray-900',
'group flex items-center px-2 py-2 text-base font-medium rounded-md'
)
}
但我不斷收到Type '({ selected }: { selected: any; }) => string' is not assignable to type 'string'.ts(2322)錯誤。
我曾嘗試明確添加一個any型別,但沒有奏效。
因為我也集成eslint到我的專案中,我什至嘗試eslint-disable-next-line @typescript-eslint/strict-boolean-expressions在selected引數上方添加規則,但無濟于事。
請誰能弄清楚我做錯了什么?
uj5u.com熱心網友回復:
你傳遞給classNameprop 的實際上是一個函式,它接受一個引數,然后呼叫classNames回傳一個字串。這就是這種型別的來源:
({ selected }: { selected: any; }) => string
你可能想要做的是這個(假設selected存在于范圍內):
className={
classNames(
selected
? 'bg-gray-100 text-gray-900'
: 'text-gray-600 hover:bg-gray-50 hover:text-gray-900',
'group flex items-center px-2 py-2 text-base font-medium rounded-md'
)
}
這會將classNameprop設定為呼叫的結果classNames,這就是您想要的 -a string。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351497.html
