簡體   English   中英

為什么 ConnectedProps 沒有推斷出正確的類型?

[英]Why is ConnectedProps not inferring the correct type?

我有一個根 redux state 定義:

interface RootState {
    users: User[]
}

在我的組件中,我想使用ConnectedProps從我的 state 映射和調度映射中自動創建道具類型:

const mapState = (state: RootState) => ({
    users: state.users
});

const mapDispatch = {
    deleteUser: (id: number) => (deleteUser(id)) // deleteUser action creator defined in another file
}

const connector = connect(
  mapState,
  mapDispatch
)

type Props = ConnectedProps<typeof connector>

但是,當我嘗試使用props.users.map(user => {...在我的組件中對用戶進行 map 時...我收到類型錯誤:

Parameter 'user' implicitly has an 'any' type

為什么將users視為Any[]而不是User[]

請注意,如果我將Props替換為

interface Props {
    users: User[]
}

只需將類型添加到我的 map function 即可解決問題:

props.users.map((user: User) => {...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM