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

Support of ES6 class constructor property #7605

Closed
blmarket opened this issue Mar 21, 2016 · 3 comments
Closed

Support of ES6 class constructor property #7605

blmarket opened this issue Mar 21, 2016 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@blmarket
Copy link

TypeScript Version:

1.7.5

Code

class Foo extends React.Component<any, any> {
 // ... some class definitions
}
Foo.defaultProps = {
  bar: 'baz'
}

Expected behavior:

No error.

Actual behavior:

TS2339: Property 'defaultProps' does not exist on type 'typeof Foo'.

Description

Hi, I'd like to create react class using typescript with ES6 classes, and encountered above error.

It would be awesome if I can set class property like above instead of class body. seems typescript does not supports it?

@Arnavion
Copy link
Contributor

The error is that by default there is no defaultProps static property on Foo. So you just have to tell the compiler that you want such a property.

class Foo extends React.Component<any, any> {
    static defaultProps: any;
}

Foo.defaultProps = {
  bar: 'baz'
}

In general, if the props was of some type P instead of any, then it would be class Foo extends React.Component<P, ... and static defaultProps: P;

@RyanCavanaugh
Copy link
Member

You can also use module merging for slightly less code:

class Foo { ... }
namespace Foo {
  export const defaultProps = {
    ...
  }
}

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Mar 21, 2016
@mhegazy mhegazy closed this as completed Mar 21, 2016
@blmarket
Copy link
Author

Thank you very much for kind answers! 👍

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants