React Component上的打字稿错误:无法将'Element'类型的参数分配给该类型的参数

时间:2020-08-07 04:09:12

标签: javascript reactjs typescript

我不确定为什么在TBH抱怨,错误使我困惑。

enter image description here

createLinkSetsForCountry

function createLinkSetsForCountry(country: Country, companies: Array<Company>) {
    let companyLinks: Array<LinkItem> = [];
    const companyLinkSets = [];

    for (const company of companies) {
        companyLinks.push(<LinkItem company={company} key={company.id} />);

        companyLinkSets.push(
            <LinkSet
                className="ft-link-set"
                country={country}
                key={company.id}
                links={companyLinks} />
        );
        companyLinks = [];
    }

    return (
        <div>
            <ListHeader country={country} />
            <div>{companyLinkSets}</div>
        </div>
    );
}

LinkItem

export class LinkItem extends Component<{ company: Company, key: number }> {
    render() {
        const { company } = this.props,
            hasApp = (company.interview
                && company.interview.app
                && company.interview.app.hasAppp),
            uri = company.notInterviewed ? `companies/${company.id}/details` : `/interviews/companies/${company.id}`,
            className = `margin-top-10 margin-bottom-10 ${
                company.notInterviewed ? 'ft-company-not-interviewed' : ''
            }`;
        const link = (
            <Link className={className} id={company.id.toString()} to={uri}>
                <span id="company-name">{company.name}</span>
            </Link>
        );

        return (
            <div className="company-link vertical-align-top inline-block">
                <div>
                    <li className="margin-0">{link}</li>
                </div>
                {hasApprenticeship && (
                    <div className="align-center ft-tags margin-0" id="tags">
                        <span
                            className="ink-tooltip ink-badge category-badge font-9 inline ft-apprenticeship-tag"
                            data-tip-color="tooltip"
                            data-tip-text="offers dddd"
                            data-tip-where="up">
              A
                        </span>
                    </div>
                )}
            </div>
        );
    }
}

1 个答案:

答案 0 :(得分:1)

基本上,您的companyLinks是React元素列表,而不是LinkItem,因此只需进行如下更改即可

let companyLinks: Array<React.ReactElement> = [];