Skip to content

Support 'static' to be used with 'abstract' and generic type #10603

Closed

Description

@k8w

TypeScript Version: 2.0.0

Code

abstract Class BaseClass<T>{
    abstract static defaultValue:T;
    // ...
}

interface Format{
    //...
}

class ClassA extends BaseClass<Format>{
    static defaultValue:Format = { ... }
}

Expected behavior:
Can define static member in abstract class, so that force child classes contains it.
Such as defaultProps in React.Component

Actual behavior:
Neither interface nor abstract class can achieve this.

Activity

  1. changed the title [-]Support 'static' to be used with 'abstract'[/-] [+]Support 'static' to be used with 'abstract' and generic type[/+] on Aug 30, 2016
  2. OliverJAsh commented on Jun 5, 2017

    @OliverJAsh
    Contributor

    This is more general than the generic type use case:

      abstract class Base {
        abstract foo: string
        static bar: string
      }
      class BaseExtended extends Base {
        foo = '1'
        // expected error because static property is missing, but got none
        // static bar = '1'
      }

    For context, I want this feature so I can properly type the static properties on my React class components. The React typings on DefinitelyTyped define a Component class, and a ComponentClass interface which includes the constructor and the static properties such as propTypes, displayName, and defaultProps. To type check a class component including its static properties, one has to define a class that extends Component and annotate it as ComponentClass:

      // error as expected, displayName should be string
      const ReactComponent: ComponentClass<any> = class extends Component<any, any> {
        static displayName = 1
        render() {
          return <div>{this.props.name}</div>
        }
      };

    If we had the feature requested here, we could consolidate the ComponentClass interface and the Component class into an abstract class with static properties.

  3. mhegazy commented on Aug 28, 2017

    @mhegazy
    Contributor

    looks like a duplicate of #14600

  4. added
    DuplicateAn existing issue was already created
    and removed
    SuggestionAn idea for TypeScript
    on Aug 28, 2017
  5. mhegazy commented on Sep 12, 2017

    @mhegazy
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  6. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions