0

I've been trying to understand how the React.FC type works and tried multiple things this being the only one that works:

import React from 'react'
    
    type iProps<P = {}> = {
        (props: P): React.ReactElement<any, any>
    }
    
    interface IPropsTest {
        name: string,
        age: number
    }
    
    const MyComponent: iProps<IPropsTest> = ({ name, age }) => {
        return <div>{name} {age}</div>
    }

Could someone explain this part

type iProps<P = {}> = {
    (props: P): React.ReactElement<any, any>
}

Why do I need <P = {}> and why do I have to put (props: P)

2
  • what are you trying to do? Your are not even using the FC type here Commented Nov 1, 2020 at 18:47
  • Do you conceptually understand Typescript generics? If not, start there. Commented Nov 1, 2020 at 18:51

1 Answer 1

1

What is P = {}

This is a generic default. If the generic argument is not provided then P is assumed to be {}.

Why do I have to put (props: P)

This means the function can be expected to be called with one argument of type P.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.