andrew-w-ross / typings-react-router

Type definitions for react-router
2 stars 19 forks source link

withRouter throws a compiler error #17

Closed bezreyhan closed 8 years ago

bezreyhan commented 8 years ago

I have a class PageHeader that uses withRouter like so:

interface IProps {
   router: { goBack: { (): void } };
}
interface IState {}

class PageHeader extends React.Component <IProps, IState> {
   ...
}

export default withRouter(PageHeader)

However, I get the following error: TS2324: Property 'router' is missing in type 'IntrinsicAttributes & IntrinsicClassAttributes<PageHeader> & IProps & { children?: {} | undefined...'.

bezreyhan commented 8 years ago

This is not a bug. The reason for this error was that I was not providing an interface for context. Adding this to my component class fixed the issue:

context: IContext

static contextTypes = { router: React.PropTypes.func.isRequired } where IContext is:

interface IContext { router: { goBack: { (): void } }; }