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

JeromeDL30 picture JeromeDL30 · Apr 3, 2019 · Viewed 10.6k times · Source

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 />

Answer

Bsalex picture Bsalex · Apr 3, 2019

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;
}