'Ant design onFinish function with typescript; Type '(values: FormData) => void' is not assignable to type '(values: unknown) => void'

I have interface FormData, which is supposed to be same type as values that onFinish function receives, but when I put

const onFinish = (values: FormData) => {

I get error Type '(values: FormData) => void' is not assignable to type '(values: unknown) => void'. I've tried faking the data and making the same object the onFinish function gets when I submit the form:

interface FormData {
    firstName: string;
    lastName: string;
    username: string;
    password: string;
    confirmPassword: string;
}

const obj: FormData = {
    firstName: 'andrija',
    lastName: 'gajic',
    username: 'radojica51',
    password: 'idegas51',
    confirmPassword: 'idegas51'
}

When doing it this way it doesn't throw any error although it's the same data the form receives

import axios from 'axios';

import { Form, FormCaption } from './Form.style';
import { Input, Button } from 'antd';

interface FormData {
    firstName: string;
    lastName: string;
    username: string;
    password: string;
    confirmPassword: string;
}

const FormComponent = () => {

    // change values type to FormData
    const onFinish = (values: any) => {
        if (values.password !== values.confirmPassword) {
            message.error('Passwords don\'t match!');
            return;
        }
        console.log(values);

        axios.post('http://localhost:5000/register', values);
    }

    return (
        <Form onFinish={onFinish}>
            <FormCaption>Register</FormCaption>

            <FormElement.Item
                label="First name"
                name="firstName"
                rules={[
                    {
                        required: true,
                        message: 'Please enter your first name!',
                    },
                ]}
            >
                <Input />
            </FormElement.Item>

            <FormElement.Item
                label="Last name"
                name="lastName"
                rules={[
                    {
                        required: true,
                        message: 'Please enter your last name!',
                    },
                ]}
            >
                <Input />
            </FormElement.Item>

            <FormElement.Item
                label="Username"
                name="username"
                rules={[
                    {
                        required: true,
                        message: 'Please enter the username!',
                    },
                    {
                        min: 6,
                        max: 20,
                        message: 'Must be between 6 and 20 characters'
                    }
                ]}
            >
                <Input />
            </FormElement.Item>

            <FormElement.Item
                label="Password"
                name="password"
                rules={[
                    {
                        required: true,
                        message: 'Please enter the password!',
                    },
                    {
                        min: 6,
                        max: 20,
                        message: 'Must be between 6 and 20 characters'
                    }
                ]}
            >
                <Input.Password />
            </FormElement.Item>

            <FormElement.Item
                label="Confirm Password"
                name="confirmPassword"
                rules={[
                    {
                        required: true,
                        message: 'Please confirm your password!',
                    },
                    {
                        min: 6,
                        max: 20,
                        message: 'Must be between 6 and 20 characters'
                    }
                ]}
            >
                <Input.Password />
            </FormElement.Item>
   
            <Button htmlType="submit">Register</Button>
        </Form>
  )
}

export default FormComponent;


Solution 1:[1]

You can use below code to handle

const onFinish = (values: FormData) => { }; 

and in onFinish use below

<Form onFinish={(values)=> onFinish(values as FormData)}>
    ....
</Form>

Lern more about as keyword here

Solution 2:[2]

I personally don't like writing jsx like this but you can pass the expected form type as so:

import { Form } from 'antd';
// ...

const FormComponent = () => {
  // ...
  const onFinish = (values: FormData) => {
    // ...
  }
  
  return (
    <Form<FormData> onFinish={onFinish}>
    // ...

Now onFinish will not complain and have correct types.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shreyans Shrivastav
Solution 2 alextrastero