Definitelytyped: Property 'propTypes' does not exist on type 'typeof TestComponent'.

Created on 7 Feb 2019  ·  6Comments  ·  Source: DefinitelyTyped/DefinitelyTyped

If you know how to fix the issue, make a pull request instead.

  • [x] I tried using the @types/xxxx package and had problems.
  • [x] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [x] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • [x] [Mention](https://github.com/blog/821-mention-somebody-they-re-notified) the authors (see Definitions by: in index.d.ts) so they can respond.

    • Authors: @johnnyreilly @bbenezech @pzavolinsky @digiguru @ericanderson @tkrotoff @DovydasNavickas @onigoetz @theruther4d @guilhermehubner @ferdaber @jrakotoharisoa @pascaloliv @hotell @franklixuefei @Jessidhia @pshrmn @threepointone

If you do not mention the authors the issue will be ignored.

import React from 'react';

import PropTypes from 'prop-types';

class TestComponent extends React.Component {}
TestComponent.propTypes = {};

Error: Property 'propTypes' does not exist on type 'typeof TestComponent'.

But no error when using the static keyword.

import React from 'react';

import PropTypes from 'prop-types';

class TestComponent extends React.Component {
  public static propTypes = {};
}

Most helpful comment

This is a standard TypeScript mechanism. With classes the expectation is that all static properties are initialized in the class definition. Adding it after the fact is only supported for functions.

All 6 comments

This is a standard TypeScript mechanism. With classes the expectation is that all static properties are initialized in the class definition. Adding it after the fact is only supported for functions.

and how would one add it after the fact with a function, because i get an error when doing it like this:

import Banner from './Banner';
...
const SubNav: React.FC<Props> = ({ className, items }) => { ... }
...
SubNav.Banner = Banner

Once you annotate a value with its type (like you did with const SubNav: React.FC<Props> you can't change its type afterwards, so you need to declare it all at once or redeclare it as another constant.

This error only serves to put people who are new to TS off gradually converting their code to TS. I'm having to litter my code with // @ts-ignore due to baffling errors like this when changing files from .js to .tsx and it catches no bugs.

In this case it catches no bugs. It catches a ton of bugs elsewhere. Type everything as “any” if you want this behavior. Also this is not an issue for DT as it is about the behavior of TS, not the types.

Please close.

I have a React class with Class.propTypes I change the module to .ts and all of a sudden it's buggy? What broke by just changing the file extension?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

variousauthors picture variousauthors  ·  3Comments

Zzzen picture Zzzen  ·  3Comments

jgoz picture jgoz  ·  3Comments

JudeAlquiza picture JudeAlquiza  ·  3Comments

JWT
svipas picture svipas  ·  3Comments