Home > OS >  typecast react typescript component class props
typecast react typescript component class props

Time:01-23

Starting with React, Nextjs with typescript

I have the following class which gives an error while accessing avatar and name

Property does not exists on type readonly

enter image description here

How can I typecast the properties properly?

CodePudding user response:

React.Component is a generic class. You can specify the props there:

interface AvatarProps {
  avatar?: string;
  name: string;
}

class Avatar extends React.Component<AvatarProps> {

If you leave it out, the default is to assume there are no props.

  •  Tags:  
  • Related