Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Component static properties #16967

Closed
OliverJAsh opened this issue Jun 5, 2017 · 3 comments · Fixed by #30730
Closed

React Component static properties #16967

OliverJAsh opened this issue Jun 5, 2017 · 3 comments · Fixed by #30730

Comments

@OliverJAsh
Copy link
Contributor

React’s Component class supports static properties such as displayName, defaultProps, propTypes, and others.

When creating a component class, the common practice seems to be to extend the Component class type. However, the Component class type does not include these static properties. This means you don't get errors for these static properties:

import React, { Component, ComponentClass } from 'react';

type Props = { name: string };

{
  class ReactComponent extends Component<Props, any> {
    // expected error, but got none: displayName should be a string
    static displayName = 1
    // expected error, but got none: defaultProps.name should be a string
    static defaultProps = { name: 1 }
  };
}

These static properties have instead been defined on a separate type, the ComponentClass interface. In order to get type checking for these static properties, one has to annotate the class using this interface:

{
  // error: displayName should be a string
  // error: defaultProps.name should be a string
  const ReactComponent: ComponentClass<Props> = class extends Component<Props, any> {
    static displayName = 1
    static defaultProps = { name: 1 }
  };
}

Is there any way we can consolidate these two types, so we get free type checking of static properties simply by extending Component?

/cc React type definition authors: @johnnyreilly @bbenezech @pzavolinsky @digiguru @ericanderson

@OliverJAsh
Copy link
Contributor Author

To address this I think we need TypeScript to have the ability to have abstract static properties/methods in abstract classes: microsoft/TypeScript#14600 (comment)

@mattmccutchen
Copy link
Contributor

mattmccutchen commented Oct 2, 2018

AFAICT, it should work to just copy the contextTypes, childContextTypes, and displayName declarations to the Component class as optional static properties. I don't see how microsoft/TypeScript#14600 is relevant, given that the properties are optional. If they were required, then we would need microsoft/TypeScript#14600.

defaultProps and propTypes cannot be copied because they would be static properties that depend on P, which is an instance type parameter. Any TypeScript suggestion to provide a way out of that problem seems likely to me to be a stretch. See #28515 for what I believe is the best current solution for defaultProps and propTypes. Note that converting to React.ComponentClass gives you checking of the defaultProps and propTypes but (according to my tests) not contextual typing, which would be highly desirable.

mattmccutchen added a commit to mattmccutchen/DefinitelyTyped that referenced this issue Oct 2, 2018
Recommend react-typescript-helpers for defaultProps and propTypes.

Fixes DefinitelyTyped#28515.
Fixes DefinitelyTyped#16967.
mattmccutchen added a commit to mattmccutchen/DefinitelyTyped that referenced this issue Nov 21, 2018
- Copy properties from ComponentClass to the Component class.

- Recommend the react-typescript-helpers package for checking the
  defaultProps and propTypes at a component definition.

- As a fallback, have JSX.LibraryManagedAttributes check the
  defaultProps and propTypes every time a component is used.

- Fix test failures in dependent packages.

Fixes DefinitelyTyped#28515.
Fixes DefinitelyTyped#16967.
mattmccutchen added a commit to mattmccutchen/DefinitelyTyped that referenced this issue Nov 21, 2018
- Copy properties from ComponentClass to the Component class.

- Recommend the react-typescript-helpers package for checking the
  defaultProps and propTypes at a component definition.

- As a fallback, have JSX.LibraryManagedAttributes check the
  defaultProps and propTypes every time a component is used.

- Fix test failures in dependent packages.

Fixes DefinitelyTyped#28515.
Fixes DefinitelyTyped#16967.
mattmccutchen added a commit to mattmccutchen/DefinitelyTyped that referenced this issue Nov 26, 2018
- Copy properties from ComponentClass to the Component class.

- Recommend the react-typescript-helpers package for checking the
  defaultProps and propTypes at a component definition.

- As a fallback, have JSX.LibraryManagedAttributes check the
  defaultProps and propTypes every time a component is used.

- Fix test failures in dependent packages.

Fixes DefinitelyTyped#28515.
Fixes DefinitelyTyped#16967.
mattmccutchen added a commit to mattmccutchen/DefinitelyTyped that referenced this issue Nov 27, 2018
- Copy properties from ComponentClass to the Component class.

- Recommend the react-typescript-helpers package for checking the
  defaultProps and propTypes at a component definition.

- As a fallback, have JSX.LibraryManagedAttributes check the
  defaultProps and propTypes every time a component is used.

- Fix test failures in dependent packages.

Fixes DefinitelyTyped#28515.
Fixes DefinitelyTyped#16967.
mattmccutchen added a commit to mattmccutchen/DefinitelyTyped that referenced this issue Nov 27, 2018
- Copy properties from ComponentClass to the Component class.

- Recommend the react-typescript-helpers package for checking the
  defaultProps and propTypes at a component definition.

- As a fallback, have JSX.LibraryManagedAttributes check the
  defaultProps and propTypes every time a component is used.

- Fix test failures in dependent packages.

Fixes DefinitelyTyped#28515.
Fixes DefinitelyTyped#16967.
johnnyreilly pushed a commit that referenced this issue Nov 30, 2018
- Copy properties from ComponentClass to the Component class.

- Recommend the react-typescript-helpers package for checking the
  defaultProps and propTypes at a component definition.

- As a fallback, have JSX.LibraryManagedAttributes check the
  defaultProps and propTypes every time a component is used.

- Fix test failures in dependent packages.

Fixes #28515.
Fixes #16967.
@johnnyreilly johnnyreilly reopened this Nov 30, 2018
@orta orta closed this as completed Jun 7, 2021
@orta
Copy link
Collaborator

orta commented Jun 7, 2021

Hi thread, we're moving DefinitelyTyped to use GitHub Discussions for conversations the @types modules in DefinitelyTyped.

To help with the transition, we're closing all issues which haven't had activity in the last 6 months, which includes this issue. If you think closing this issue is a mistake, please pop into the TypeScript Community Discord and mention the issue in the definitely-typed channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants