TypeScript 错误类型‘{}’缺少以下类型的属性
2020-09-18
4374
我刚接触 TypeScript,但遇到了这个错误。
Type '{}' is missing the following properties from type 'Pick<PropsI, "loadUser" | "user">': loadUser, user
上述错误来自 App.text 并指向
<AppRouter/>
我很困惑为什么会遇到这个问题,可能的解决方法是什么?我假设我没有在应用路由器中正确声明某些内容,但我似乎无法弄清楚哪里出了问题或我遇到了什么错误?
App.tsx
import AppRouter from './router/AppRouter'
const App: React.FC = () => {
return (
<Fragment>
<AppRouter />
</Fragment>
)
}
export default App
应用路由器
import React, {ExoticComponent, Fragment, useEffect} from 'react'
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom'
import FreeTextView from '../components/analytics/dashboard/freeTextView/FreeTextView'
import {connect, useDispatch, useSelector} from 'react-redux'
import {Nav, IconSvgs} from '@pxhub/shared-components'
import {AppState, UserI, AccessRecordsI} from '../store/actions/Types'
import {loadUser} from '../store/actions/UserActions'
interface PropsI {
loadUser: () => void
user: UserI
}
// Defualt Page
const Page = () => {
return (
<Fragment>
<Switch>
<Route path="/freeview" component={FreeTextView} />
</Switch>
</Fragment>
)
}
// App Router
export const AppRouter: React.FC<PropsI> = ({loadUser, user}: PropsI) => {
const userAccess = user.accessRecords.map((accessRecord: AccessRecordsI) => {
return accessRecord.Access_Transaction
})
useEffect(() => {
loadUser()
})
return (
<Fragment>
<IconSvgs />
<Nav
user={user}
userAccess={userAccess}
urlPrefix={process.env.REACT_APP_URL_PREFIX}
/>
<Router>
<Switch>
<Route path="/" component={Page} />
</Switch>
</Router>
</Fragment>
)
}
const dispatch = useDispatch()
const mapDispatchToProps = () => () => {
dispatch(loadUser())
}
const mapStateToProps = useSelector((state: AppState) => state.user)
export default connect(mapStateToProps, mapDispatchToProps)(AppRouter)
2个回答
您声明 AppRouter 需要 props loaduser 和 user,但随后您在 App.tsx 中调用它时没有这些 props。
编辑:虽然这不是问题中确定的特定 TS 错误的原因,但如评论中所述,它也会导致将
useSelector
和
mapStateToProps
或
useDispatch
和
mapDispatchToProps
结合起来出现问题。这些天您可能只想使用钩子。
sam256
2020-09-18
我不太清楚 mapDispatchToProps 的语法。
难道它不应该是这样的吗?
const mapDispatchToProps = () => {
dispatch(loadUser())
}
Davide Turini
2020-09-18