Home > front end >  How to define generc to FC component in typescript?
How to define generc to FC component in typescript?

Time:09-17

What syntax to pass type to Interface Props generic? (Cat must be type FC)

interface CatProps<T> {
  value: T
}

const Cat: FC<CatProps<T>> = () => {
  return <h1>Hello World!</h1>
}

const cat = <Cat<number> />

enter image description here

CodePudding user response:

Here's a complete example (based on @Alex Chashin's comment):

interface CatProps<T> {
  value: T;
}

const Cat = <T extends any>({value}: CatProps<T>) => <h1>Hello {value}!</h1>

const cat = Cat({value: 'world'});

or if you want to pass a number:

const cat = Cat({value: 11});
  • Related