我正在鍵入以呼叫具有 defaultProptypes 的函式來驗證
interface testProps {
name: string;
state?: 'ca' | 'ny' | 'fl';
}
Testing.defaultProps = {
state: 'ca',
};
const Test1 = ({ name }: testProps) => {
return (
<div>{name}</div>
)}
export const Testing = ({ name, state }: testProps) => {
return (
<>
<Test1 name={name} />
</>
);
};
在上面的例子中,測驗功能運行良好。將名稱值傳遞給 Test1 函式時出現錯誤,因為型別檢查將由“testProps”執行,但現在名稱的值只是一個字串。如何對 Test1 函式使用相同的 testProps?
錯誤——
'state' PropType is defined but prop is never used react/no-unused-prop-types
propType "state" is not required, but has no corresponding defaultProps declaration
uj5u.com熱心網友回復:
Typescript 對你的代碼很滿意,但 ESlint 不滿意。 react/no-unused-prop-types是一個eslint 錯誤。
您可以testProps通過將Test1的 arg 型別縮小為Pick<testProps, 'name'>.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/364357.html
