Reactjs 使用Typescript进行Redux连接-TS2347
我对打字脚本还不熟悉,虽然它很吸引人,是个救命稻草,但这个错误很难破解 TypeScript错误:非类型化函数调用可能不接受类型参数。TS2347 你能告诉我在下面的课程中应该改进什么来消除这个错误吗 这是全班同学Reactjs 使用Typescript进行Redux连接-TS2347,reactjs,typescript,react-redux,Reactjs,Typescript,React Redux,我对打字脚本还不熟悉,虽然它很吸引人,是个救命稻草,但这个错误很难破解 TypeScript错误:非类型化函数调用可能不接受类型参数。TS2347 你能告诉我在下面的课程中应该改进什么来消除这个错误吗 这是全班同学 import React, { FunctionComponent } from 'react' import { ListGroup } from 'react-bootstrap' import { connect } from 'react-redux' type Sta
import React, { FunctionComponent } from 'react'
import { ListGroup } from 'react-bootstrap'
import { connect } from 'react-redux'
type StateProps = {
mbzArtists: IMBZArtist[],
releaseArtistID: string
}
type DispatchProps = {
findMBZReleases: (artistID: string) => void,
}
type OwnProps = {}
type MBZSearchResultsProps = StateProps & DispatchProps & OwnProps
const MBZSearchResults: FunctionComponent<MBZSearchResultsProps> = ({ findMBZReleases, mbzArtists, releaseArtistID }) => {
return (
<div className="MBZSearchResults">
// div content
</div>
)
}
const mapStateToProps = (state: AppState) => {
return {
mbzArtists: state.musicBrainz.mbzArtists,
releaseArtistID: state.musicBrainz.artistReleaseID
}
}
const mapDispatchToProps = (dispatch: any): DispatchProps => {
return {
findMBZReleases: (artistID: string) => dispatch(Actions.MBZActions.findMBZReleases(artistID))
}
}
export default connect<StateProps, DispatchProps, OwnProps>(mapStateToProps, mapDispatchToProps)(MBZSearchResults)
import React,{FunctionComponent}来自“React”
从“react bootstrap”导入{ListGroup}
从“react redux”导入{connect}
类型StateProps={
mbzArtists:IMBZArtist[],
releaseArtistID:string
}
类型DispatchProps={
findMBZReleases:(artistID:string)=>void,
}
键入OwnProps={}
类型MBZSearchResultsProps=StateProps&DispatchProps&OwnProps
const MBZSearchResults:FunctionComponent=({findMBZReleases,mbzArtists,releaseArtistID})=>{
返回(
//div内容
)
}
常量mapStateToProps=(状态:AppState)=>{
返回{
mbzArtists:state.musicBrainz.mbzArtists,
releaseArtistID:state.musicBrainz.artistReleaseID
}
}
const mapDispatchToProps=(调度:任意):调度道具=>{
返回{
findMBZReleases:(artistID:string)=>dispatch(Actions.MBZActions.findMBZReleases(artistID))
}
}
导出默认连接(mapStateToProps、mapDispatchToProps)(MBZSearchResults)
如果你需要更多的信息,请告诉我
谢谢。如您发布的错误所述 TypeScript错误:非类型化函数调用可能不接受类型 论据。TS2347 connect函数不接受类型参数 将导出连接更改为:
export default connect(mapStateToProps, mapDispatchToProps)(MBZSearchResults)
通过如下更改出口声明,似乎可以解决此问题:
const组件:React.FunctionComponent=
连接(mapStateToProps、mapDispatchToProps)(MBZSearchResults)
导出默认组件
能否请typescript用户发表评论,让我知道这是否是可行的方法?如果我们这样做,那么就不会对该组件进行任何道具验证,并且可以在调用它时放入任意数量的道具。我已经试过了,在开始之前,我已经研究过这个组件的类型和编写方法。您是否尝试过这一点?通过如下更改导出语句,似乎可以解决此问题:const component:React.FunctionComponent=connect(mapStateToProps,mapsDispatchToprops)(MBZSearchResults)导出默认组件能否请typescript用户发表评论,让我知道这是否可行?我理解这是一种分离组件将使用的公共和内部道具的方法。公共道具是由其他组件/用户干预更改/传递的道具。内部/私有道具是以某种方式继承的道具;例如,如果使用react导航,则使用“导航”道具。因此,您不希望您的用户能够搞乱这些内部道具。