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

Revaluate default props behavior in JSX functional components #36433

Open
svpace opened this issue Jan 25, 2020 · 3 comments
Open

Revaluate default props behavior in JSX functional components #36433

svpace opened this issue Jan 25, 2020 · 3 comments
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@svpace
Copy link

svpace commented Jan 25, 2020

Regarding the recognition of default initializers as part of type checking on functional components, I believe issues #32402 and #31247 should be reevaluated in the light of #34547 and the potential deprecation of defaultProps, and also on the own merits of both issues.

TypeScript Version: 3.7.5

Search Terms:
defaulProps, FunctionalComponent, JSX, React

Code
View issues #32402 and #31247

Expected behavior:
View issues #32402 and #31247

Actual behavior:
View issues #32402 and #31247

Playground Link:
View issues #32402 and #31247

Related Issues:
Issues #32402 and #31247 =D

@DanielRosenwasser
Copy link
Member

This is pretty open-ended - what re-evaluation did you have in mind?

@svpace
Copy link
Author

svpace commented Jan 25, 2020

Whoa.. that was fast. Thank you for that.

Both issues were closed on a "working as intended" basis (although only #32402 was labelled). Given the following code extracted from #31247:

interface Props {
  name: string;
  optional: string;
}
const Component = ({ name, optional = "default" }: Props) => (
  <p>{name + " " + optional}</p>
);
const Test = () => <Component name="test" />;

IMHO it is safe to say that both, the "Component" function definition and the "Props" interface agree that "optional" should not be undefined. Even if rejecting "Test" definition is really working as intended, it is at least a bit surprising.

Although there are workarounds, including using "defaultProps", I believe it is undesirable to have to change a theoretically type safe code only to satisfy the type checker.

Now, since #34547 deals with, among other things, the possible deprecation of "defaultProps", it seemed like a good opportunity reevaluate supporting default initializers as an alternative, and a much less surprising solution, to default props on functional components.

Sorry for the long reply.

Regards

@svpace
Copy link
Author

svpace commented Jan 25, 2020

It seems #27425 also related

@RyanCavanaugh RyanCavanaugh added Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript labels Jan 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants