Typescript: Type error with React.Component<void, void>

Created on 27 Apr 2017  ·  5Comments  ·  Source: microsoft/TypeScript



TypeScript Version: 2.3.1

Code

export class NavMenu extends React.Component<void, void> {...}

<NavMenu />

Expected behavior:

It doesn't give a type error.

Actual behavior:

At the location the component is instantiated:

      TS2322: Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<NavMenu> & { children?: ReactNode; } & void'.
    Type '{}' is not assignable to type 'void'.

This could be a typings error perhaps, but it seems wrong to have to create a functional component to not have any props, or have to make the props type undefined?

Duplicate

Most helpful comment

Just ran into this same issue after updating. Thanks!

Side note: I thought it was standard to use "void" as the state/props type when they don't exist. My guess is a lot of people will have broken builds when they upgrade.

All 5 comments

Why don't you use React.Component<{}, {}>?

as @aluanhaddad noted, void for props does not make much sense. props will always be there at run time and it will be an object. it should React.Component<{}, {}>.

Ok thanks!

Just ran into this same issue after updating. Thanks!

Side note: I thought it was standard to use "void" as the state/props type when they don't exist. My guess is a lot of people will have broken builds when they upgrade.

Was this page helpful?
0 / 5 - 0 ratings