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]: defaultProps doesn't work with function component - LibraryManagedAttributes #29816
Comments
Interesting, I had thought that TS 3.1 allows static properties to be picked up by the typechecker, to prevent exactly this bug? What's the result of this type check: function foo() {}
foo.defaultProps = 'abc'
// expecting 'string' here
type DefaultPropsOfFoo = typeof foo extends { defaultProps: infer D } ? D : never |
related -> microsoft/TypeScript#27425 so it's gonna be fixed in 3.2 but before that a whole react types needs to be revamped to be compatible with latest |
fixed in 3.2 and react types 16.7.x |
@Hotell Is the fix available in |
Yup make sure your editor consumes proper TS version |
Do we have to declare the VSCode is properly configured EDIT: Tried it your way and it works, but I prefer using |
Works for me both ways. If in doubt restart tsserver or even better execute tsc from terminal ( I highly recommend using my pattern:
To learn more check: https://medium.com/@martin_hotell/10-typescript-pro-tips-patterns-with-or-without-react-5799488d6680
SFC is dead. You should use |
Yup just realized that SFC is deprecated. I'll use your pattern if it's the only way to make it works with defaultProps of FunctionComponent, and it also has valid points. Thanks for your article btw! I put my repro here https://github.com/panjiesw/react-ts-32 in case you want to try it. I still can't get it to work with |
I'll create new issue as this issue was for core functionality described here |
If you know how to fix the issue, make a pull request instead.
I tried using the
@types/react
package and had problems.I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
Dependencies:
Code Sample:
Current Behaviour:
<Button onClick={handleClick}>Click me</Button>
will get compile errorFollowing code works with class component as expected:
Expected Behaviour:
Make default props work for both class and function components
The text was updated successfully, but these errors were encountered: