0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to do this while deconstructing the props? It doesn't seem like React.Component<{ x, y }: CustomProps> works like it does with functional components

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.