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

[@types/react] React.creatContext should receive a optional value #34854

Closed

Conversation

wszgxa
Copy link
Contributor

@wszgxa wszgxa commented Apr 19, 2019

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Add or edit tests to reflect the change. (Run with npm test.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

DOC: https://reactjs.org/docs/context.html#reactcreatecontext

The defaultValue argument is only used when a component does not have a matching Provider above it in the tree.

And there is an article with this issue, use a tricky way to avoid this problem.

@typescript-bot typescript-bot added this to Needs Author Attention in Pull Request Status Board Apr 19, 2019
@typescript-bot typescript-bot added the Popular package This PR affects a popular package (as counted by NPM download counts). label Apr 19, 2019
@typescript-bot
Copy link
Contributor

typescript-bot commented Apr 19, 2019

@wszgxa Thank you for submitting this PR!

🔔 @johnnyreilly @bbenezech @pzavolinsky @digiguru @ericanderson @tkrotoff @DovydasNavickas @onigoetz @theruther4d @guilhermehubner @ferdaber @jrakotoharisoa @pascaloliv @Hotell @franklixuefei @Jessidhia @pshrmn @saranshkataria @lukyth @eps1lon - please review this PR in the next few days. Be sure to explicitly select Approve or Request Changes in the GitHub UI so I know what's going on.

If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead.

@typescript-bot
Copy link
Contributor

typescript-bot commented Apr 19, 2019

@wszgxa The Travis CI build failed! Please review the logs for more information.

Once you've pushed the fixes, the build will automatically re-run. Thanks!

@eps1lon
Copy link
Collaborator

eps1lon commented Apr 19, 2019

Isn't the problem with this that I can create a context with const Context = createContext<SomeValue>() and then useContext(Context) would return SomeValue instead of SomeValue | undefined? That doesn't sound desirable. I'd rather have opt-out of strict types instead of opt-in to strict types.

@ferdaber
Copy link
Contributor

@eps1lon 's reasoning is correct. We chose to explicitly not include defaultValue as optional even when considering the docs. The React team also does not consider defaultValue to be itself optional. Let me see if I can pull up the old PR...

@ferdaber
Copy link
Contributor

#24509 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Popular package This PR affects a popular package (as counted by NPM download counts).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants