How can I type the props in the react typescript ? I’m getting the error Property ‘authors’ does not exist on type ‘[Props] & { children?: ReactNode; }’.ts. I’m expecting to pass an array wich consists of name and password keys.
type Props = {
authors: {
name: string;
password: string;
}[];
};
const Login: FC<[Props]> = ({ authors }) => {
return (
...
)
}
, even though I’m passing the props.
4
Answers
Try changing the
[Props]
toProps
like followingNow it will work because it’s expecting object not array, we will pass an object like
<Login authors={authors} />
. Now authors will be an array of objects.You defined IUser interface and defined name,password states with IUser type that’s will cause a problem because when you define this:
and that’s wrong because name,password is a string so try to define it like this:
then you have to fix passing authors to your FC component:
last thing you have to fix this type to match the type of data you passed it from App.tsx:
and you passed it from App like this:
You can either use
type
orinterface
to describe props.Usually
interface
is used for describingobjects
.It’s also a nice convention to start
interfaces
with letterI
andtypes
withT
.In the above example, the
Login
component expects a propauthors
that is an array ofobjects
described byIAuthor
e.g.Hope this helps.