Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React with Typescript - Type { } is missing the following properties from type

I'm trying to learn React with TypeScript, and I seem to keep running into TS errors that are a bit vague.

I have code written below in three files, which works fine when compiled and run. I just keep getting this error thrown by TypeScript that is super annoying

"Type '{ id: any; key: any; }' is missing the following properties from type 'ProfileCardProps': login, name"

// Form.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>
    );
  };

}

// Card.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;

//ProfileList

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>
    )
  };
}
like image 598
Cactusman07 Avatar asked Mar 13 '19 03:03

Cactusman07


People also ask

Is missing in type TypeScript?

The TypeScript error "Property is missing in type but required in type" occurs when we do not set all of the properties an object of the specified type requires. To solve the error, make sure to set all of the required properties on the object or mark the properties as optional.

What type has no properties in common?

The error "Type 'X' has no properties with type 'Y'" occurs when we try to assign anything to a weak type when there's no overlap in properties. To solve the error, declare any overlapping properties if they exist or use a type assertion. Here is an example of how the error occurs.


2 Answers

As you are passing card to ProfileCard component, its passing 4 values in props.

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

But your interface has only 2

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

Adding key and id should solve the issue.

interface ProfileCardProps{ 
  login: string; 
  name: string;
  id:any;
  key: any;
}
like image 98
Amol B Jamkar Avatar answered Oct 05 '22 02:10

Amol B Jamkar


This type of use of an interface requires that all members of the interface be null

interface ProfileCardProps{ 
  login: string; 
  name: string;
  id?:number; 
}
like image 20
saeed bagheri Avatar answered Oct 03 '22 02:10

saeed bagheri