reactjs 与类型脚本React-类型{ }缺少类型的以下属性

gcuhipw9  于 2023-02-08  发布在  React
关注(0)|答案(3)|浏览(89)

我正在尝试学习React with TypeScript,我似乎一直遇到有点模糊的TS错误。
我在下面的三个文件中编写了代码,编译和运行时工作正常。我只是不断得到TypeScript抛出的这个错误,超级烦人
“类型”{ id:任意;键:任意;“}"缺少类型”ProfileCardProps“的以下属性:登录,名称”
//表格.tsx

import * as React from 'react';
import Axios from 'axios';

export default class Form extends React.Component<any,any>{
  constructor(props: any){
    super(props);
    this.state = { userName: ""};
  }

  handleSubmit = (event: React.FormEvent<EventTarget>): void => {
    event.preventDefault();

   //get request...
    .then(resp => {
        this.props.onSubmit(resp.data);
    console.log(resp.data);
    this.setState({userName: ''});
  };

  public render() {
    return(
      <div>
        <div className="col-sm-12">
          <form onSubmit={this.handleSubmit}>
            <label>Run lookup:<br />
              <input type="text"
                value = {this.state.userName}
                onChange = {(event) => this.setState({userName: event.target.value})}
                placeholder = "Enter Username..." >
              </input>
            </label>
            <button type="submit">Add user info</button>
          </form>
          <br />
        </div>
      </div>
    );
  };

}

//卡片.tsx

import * as React from 'react';

interface ProfileCardProps{ 
  login: string; 
  name: string; 
}

const ProfileCard = (props: ProfileCardProps) => {

  return(
    <div className="card col-xs-1 col-sm-6 col-md-4 col-lg-3">
      <div className="profileWrapper">
        <div className="userName">
          <p>{props.login}</p>
        </div>
        <div className="user">
          <h3>{props.name}</h3>
        </div>
      </div>
    </div>
  )
}

const CardList = (props: { cards: { map: (arg0: (card: any) => JSX.Element) => React.ReactNode; }; }) => {
    return(
      <div className="row">

         // This is the line that is throwing the error
        {props.cards.map((card: { id: any; }) => <ProfileCard key={card.id} {...card} />)}
      </div>
    )
}

export default CardList;

//配置文件列表

import * as React from 'react';
import Form from './Form';
import CardList from './ProfileCard';

import "./ProfileStyles.scss";

export default class Profiles extends React.Component<any, any>{
state = {
    cards: [
      { login: "exampleLogin",
        name:"exampleName",
        key: 1
      },
      { login: "exampleLogin2",
        name:"exmapleName2",
        key: 2
      }
    ]
  }

  addNewCard = (cardInfo: any) => {
    this.setState((prevState: { cards: { concat: (arg0: any) => void; }; }) => ({
      cards: prevState.cards.concat(cardInfo)
    }));
  }

  public render() {
    return(
      <div className="cardSection">
        <h2>Profiles</h2> 
        <Form onSubmit={this.addNewCard} />
        <CardList cards={this.state.cards} />
      </div>
    )
  };
}
gpfsuwkq

gpfsuwkq1#

当你传递卡片给ProfileCard组件时,它传递4个属性值。

{login: string, name: string, key: number, id: number}

但你的界面只有两个

interface ProfileCardProps{ 
  login: string; 
  name: string; 
}

添加key和id应该可以解决这个问题。

interface ProfileCardProps{ 
  login: string; 
  name: string;
  id:any;
  key: any;
}
gijlo24d

gijlo24d2#

这种类型的接口使用要求接口的所有成员都为null

interface ProfileCardProps{ 
  login: string; 
  name: string;
  id?:number; 
}
0yycz8jy

0yycz8jy3#

我已经花了大约一个小时来找出类似的错误信息,终于明白,我忘了导入适当的接口:)也许这将是有帮助的人...

相关问题