Redux ConnectedProps 类型为 never

Ale*_*mez 5 typescript reactjs redux react-redux

我正在尝试使用官方文档指南将组件连接到我的 Redux 商店,但连接的道具似乎是类型never

到目前为止,这是我的代码:

类型定义:

export interface CardInterface {
    id: string;
    titulo: string;
    descripcion: string;
    url?: string;
    fecha: Date;
}

export type CardsState = Readonly<{
    cards: CardInterface[];
}>;
Run Code Online (Sandbox Code Playgroud)

实际组件:

import * as React from 'react';
import { connect, ConnectedProps } from 'react-redux';

const mapStateToProps = (state: CardsState) => ({
    cards: state.cards,
});

const connector = connect(mapStateToProps, null);

type Props = ConnectedProps<typeof connector>;

const CardList: React.FC<Props> = ({ cards }) => {
    return (
        <div className="grid-container container">
            {cards.map((card: CardInterface) => (
                <Card
                    title={card.titulo}
                    description={card.descripcion}
                    image={card.url}
                />
            ))}
        </div>
    );
};

export default connector(CardList);
Run Code Online (Sandbox Code Playgroud)

当我尝试遍历卡片时,linter 将cards道具标识为never

在此处输入图片说明

有没有其他人遇到过这个问题?非常感谢。

ita*_*fna 7

省略未使用的mapDispatchToProps参数应该可以修复此 Typescript 错误:

const connector = connect(mapStateToProps); //instead of passing null 
Run Code Online (Sandbox Code Playgroud)

显然这是一个已知问题

  • 该死,原来这么简单。非常感谢! (2认同)