Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "Type '{}' is missing in the following properties..." error in Typescript?

I'm new to Typescript and therefore having a problem about it. I'm using Ant Design and followed how to use Form in Typescript but with FunctionComponent; however, I'm getting an error thrown by Typescript:

TypeScript error: Type '{}' is missing the following properties from type 'Readonly<RcBaseFormProps & Pick<SetupFormProps, "username" | "email" | "password" | "confirm_password" | "first_name" | "last_name">>': username, email, password, confirm_password, and 2 more. TS2740

Here's the code:

import React, { useState } from 'react';
import { Form, Input, Row, Col } from 'antd';
import { FormComponentProps } from 'antd/lib/form';


interface SetupFormProps extends FormComponentProps {
  username: string;
  email: string;
  password: string;
  confirm_password: string;
  first_name: string;
  last_name: string;
}

const SetupForm: React.FC<SetupFormProps> = ({ form }) => {
  ...
  return (
    <Form id="setup-form" layout="vertical" onSubmit={handleSubmit}>...</Form>
  )
}

export default Form.create<SetupFormProps>({ name: 'register' })(SetupForm);

and in my other component, I'm accessing it this way:

import SetupForm from './form';

<SetupForm />
like image 266
JeromeDL30 Avatar asked Apr 03 '19 17:04

JeromeDL30


1 Answers

All the props in your props interface are required (they can't be undefined)

interface SetupFormProps extends FormComponentProps {
  username: string;
  email: string;
  password: string;
  confirm_password: string;
  first_name: string;
  last_name: string;
}

But you are using your component without specifying the props from the interface

<SetupForm />

So you should either specify the props from the interface (SetupFormProps)

<SetupForm username="myUserName" ...etc />

or make the props optional

interface SetupFormProps extends FormComponentProps {
  username?: string;
  email?: string;
  password?: string;
  confirm_password?: string;
  first_name?: string;
  last_name?: string;
}
like image 144
Bsalex Avatar answered Sep 21 '22 08:09

Bsalex