Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - How to define props with typescript

I have a demo here

It a simple todo app in React using typescript.

I'm trying to define the props in typescript.

I have an interface in the Todo component for the props being passed in

If I try to access text in the Todo component I get an error saying

Property 'text' does not exist on type 'string'.

How do I define the props correctly using typescript

like image 216
cdmt Avatar asked Oct 24 '25 05:10

cdmt


1 Answers

You're defining todo as a string, but you're using it as an object that contains a text property as a string. Therefore, you props definition should be like this:

interface IProps {
  index: number,
  todo: { text: string }
}
like image 85
Nick Avatar answered Oct 25 '25 19:10

Nick