In React, props can be required or optional. TypeScript helps ensure that required props are always passed to your component, and optional props are never assumed to exist in your component logic.
Hover your mouse over <MyChildComponent />
on the right and read the error:
This error is TypeScript telling you that the name
prop is required by <MyChildComponent />
.
You can make props optional by adding ? after the property name.
type Props = {
myProps?: string
}
name
prop optional, then read the error that appears on line 10.<p>
tag only if name
was supplied in the props.Ask me about the errors you see, or anything else you need help with: