1

Why would this throw the error below?

export default class DeleteModal extends React.Component<DeleteModalProps, void>

Error:

Type 'void' is not assignable to type 'Readonly<{}>'

1
  • But why the error? Commented Apr 11, 2020 at 16:42

1 Answer 1

3

Solution

If we don't need state in the component, don't assign it would be fine.

React.Component<Props>

You can also set the empty object

interface State {}

React.Component<Props, State>

Reason

  1. If you check the source of React.Component index.d.ts
interface Component<P = {}, S = {}, SS = any> extends ComponentLifecycle<P, S, SS> { }

You can see that it's been defined as empty Object {}

  1. As for the Typescript document of void

Declaring variables of type void is not useful because you can only assign null or undefined to them

That's the reason for that error.

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

Comments

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.