Max
Max

Reputation: 1175

Typescript React component with propTypes

I have a class that extends the React.Component class and I'm passing it my own interface for the props that I expect to receive. The problem is that when I use the SomeComponent.propTypes = {... the webstorm linter says something is wrong, stating that Property 'propTypes' does not exist on type 'typeof SomeComponent'.

I have no Idea why that happends....

the code of SomeComponent is

class SomeComponent extends React.Component<IProps, {}> {...}

A similar exception is thrown when I write the following code to create a functional component:

const SomeComponent: React.FunctionComponent<IProps> = (props) => {...}

In this case, again I'm getting an error when using the SomeComponent.propTypes = {...} syntax... this time the exception is type ... is not assignable to type ....\

Any help on why it happens and how to tell typescript that I should be able to use this property? Thanks!

Upvotes: 0

Views: 1087

Answers (1)

Rengers
Rengers

Reputation: 15218

Typescript doesn't allow adding static properties after declaring the class. I believe you can add propTypes by adding it as a static property in the class:

class SomeComponent extends React.Component<IProps, {}> {
  static propTypes = {} // Set your propTypes here
}

Aside, why do you want to use propTypes when you are using Typescript? TS will take care of the type checking at compile time.

Upvotes: 3

Related Questions