1

Let's say I have a React component called Link:

function Link(props) {
  return <a href={props.url}>{props.children}</a>
}

How do I type this function without turning it into an arrow-function?

This doesn't seem to work:

interface ILink {
  url: string
}

function Link(props: React.FC<ILink>) { }

// Property 'url' does not exist on type 'FC<ILink>'.
// Property 'children' does not exist on type 'FC<ILink>'.

But as an arrow-function it instantly does work?

interface ILink {
  url: string
}

const Link: React.FC<ILink> = (props) => { }

// Compiles

1 Answer 1

4

You have to use the combo React.PropsWithChildren and React.ReactElement so it becomes:

function Link(props: PropsWithChildren<ILink>): ReactElement {}
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.