Definitelytyped: @types/react-router-dom causing compiling errors

Created on 24 Jun 2017  路  8Comments  路  Source: DefinitelyTyped/DefinitelyTyped

I tried using the @types/react-router-dom package and had problems. Using tsc -v 2.2.2 but recent npm install has updated the react-router type with definition which causes typescript error:

ERROR in c:\MO\services\flux\Websitenode_modules\@types\react-router\index.d.ts
(40,35): error TS2314: Generic type 'Component' requires 2 type argument(s).

In index.d.ts
// Type definitions for React Router 4.0
.
.
export class Route extends React.Component { }

Am I missing something? Thanks.

Most helpful comment

Thanks but it didn't work for me. I have to back down the version of react-router-dom 4.0.3 and react-router 4.0.10 to get it working.

All 8 comments

as mentioned in #17437, fixed in version 4.0.5.
this worked for me: npm install -D @typings/[email protected]

Thanks but it didn't work for me. I have to back down the version of react-router-dom 4.0.3 and react-router 4.0.10 to get it working.

Had the same experience as @tak0209 - also had to reference @types/[email protected].

Using tsc -v 2.2.2

You need TypeScript 2.3+ for the latest types/react/index.d.ts and friends

Same here with TS2.4.1, had no luck neither with [email protected] nor 4.0.3. :\

Fixed the required types manually for now. 馃惤

@soualid would you be so kind and share what was the error was that you were getting please? Was it something like?

error TS2605: JSX element type 'Redirect' is not a constructor function for JSX elements.
  Types of property 'setState' are incompatible.
    Type '{ <K extends never>(f: (prevState: void, props: RedirectProps) => Pick<void, K>, callback?: (() =...' is not assignable to type '{ <K extends never>(f: (prevState: {}, props: any) => Pick<{}, K>, callback?: (() => any) | undef...'.
      Types of parameters 'f' and 'f' are incompatible.
        Type '(prevState: {}, props: any) => Pick<{}, any>' is not assignable to type '(prevState: void, props: RedirectProps) => Pick<void, any>'.
          Types of parameters 'prevState' and 'prevState' are incompatible.
            Type 'void' is not assignable to type '{}'.

And what was your fix? I've done a hacky unconsidered "fix" to change export class Redirect extends React.Component<RedirectProps, void> { } to export class Redirect extends React.Component<RedirectProps, {}> { }

@AJamesPhillips I've encountered the same problem, and installing @types/react-router along with @types/react-router-dom fixed the issue.

All in all this should be default because package @types/react-router-dom defines in its package.json dependency for @types/react-router.

That fix doesn't seem to work if react v16 is being used.

Was this page helpful?
0 / 5 - 0 ratings