使用任意數量的道具定義任何型別的簡單反應功能組件時出現打字稿錯誤,包括使用 FC、ComponentWithChildren 等...
這是一個最小的復制
type Props = {
className?: string;
};
const Horizontal: Props = ({ className = undefined }) => (
<div />
);
給我錯誤
型別 '({ className }: { className?: undefined; }) => Element' 與型別 'Props'.ts(2559) 沒有共同的屬性
我不明白這就是我一直定義組件的方式。打字稿中有什么我不知道的新東西嗎?
{
"name": "call-mvp",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"@types/classnames": "^2.3.1",
"@types/jest": "^27.5.1",
"@types/node": "^16.11.36",
"@types/react": "^18.0.9",
"@types/react-dom": "^18.0.4",
"@types/react-measure": "^2.0.8",
"@types/react-router-dom": "^5.3.3",
"@types/styled-components": "^5.1.25",
"classnames": "^2.3.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-measure": "^2.5.2",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"styled-components": "^5.3.5",
"typescript": "^4.6.4",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
uj5u.com熱心網友回復:
您可以將其更改為:
const Horizontal: FC = ({ className = undefined } : Props)
uj5u.com熱心網友回復:
這應該作業:
const Horizontal = ({ className = undefined } : Props) => ....
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/479134.html
