Skip to content

react: change default type value of snapshot to any#24987

Merged
RyanCavanaugh merged 4 commits intoDefinitelyTyped:masterfrom
jacobwgillespie:react-snapshot-default
Apr 19, 2018
Merged

react: change default type value of snapshot to any#24987
RyanCavanaugh merged 4 commits intoDefinitelyTyped:masterfrom
jacobwgillespie:react-snapshot-default

Conversation

@jacobwgillespie
Copy link
Copy Markdown
Contributor

In the react types, the definition of React.Component is React.Component<P = {}, S = {}, SS = never>, meaning that type-wise, props defaults to {}, state defaults to {}, and snapshot defaults to never. Snapshot is used as the return value of the new React getSnapshotBeforeUpdate lifecycle method, whose return type is SS | null.

What that means, however, is that unless the third generic type parameter is overridden, the return value of getSnapshotBeforeUpdate is resolved to never | null, which is resolved to null. This causes an issue where any components that do define getSnapshotBeforeUpdate are unable to be used by something expecting a React.Component, because getSnapshotBeforeUpdate returns something other than null.

This PR changes the default to be SS = any, which allows other libraries to do things like the following:

const func = (comp: React.Component<any>) => {...}

There are many third-party libraries that do something similar, which I believe necessitates this change. Additionally, at the moment it is not possible to even use a component that uses the new lifecycle method as it is not considered to be a valid JSX component type (for the same reason, see #24820).

I have added several test cases to catch actually using components that contain getSnapshotBeforeUpdate, using them with React.createElement, and using the new lifecycle method on PureComponent, which previously did not accept the SS type param.

Fixes #24820. See #24820 for more context. Replaces #24985.


  • 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).
    If changing an existing definition:
  • Provide a URL to documentation or source code which provides context for the suggested changes: https://reactjs.org/docs/react-component.html#getsnapshotbeforeupdate
  • Increase the version number in the header if appropriate.
  • If you are making substantial changes, consider adding a tslint.json containing { "extends": "dtslint/dt.json" }.

@typescript-bot typescript-bot added Popular package This PR affects a popular package (as counted by NPM download counts). Awaiting reviewer feedback labels Apr 14, 2018
@typescript-bot
Copy link
Copy Markdown
Contributor

typescript-bot commented Apr 14, 2018

@jacobwgillespie Thank you for submitting this PR!

🔔 @johnnyreilly @bbenezech @pzavolinsky @digiguru @ericanderson @morcerf @tkrotoff @DovydasNavickas @onigoetz @theruther4d @guilhermehubner @JoshuaKGoldberg @jrakotoharisoa - 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 typescript-bot added the Unmerged The author did not merge the PR when it was ready. label Apr 19, 2018
@typescript-bot
Copy link
Copy Markdown
Contributor

After 5 days, no one has reviewed the PR 😞. A maintainer will be reviewing the PR in the next few days and will either merge it or request revisions. Thank you for your patience!

@RyanCavanaugh RyanCavanaugh merged commit c9de295 into DefinitelyTyped:master Apr 19, 2018
@RyanCavanaugh
Copy link
Copy Markdown
Member

🌟 🎈 🎉 🏆 🎂 ✨ ⭐️

Congratulations on your first DefinitelyTyped contribution!

🌟 🎈 🎉 🏆 🎂 ✨ ⭐️

@jacobwgillespie jacobwgillespie deleted the react-snapshot-default branch April 19, 2018 22:19
cmaddalozzo pushed a commit to cmaddalozzo/DefinitelyTyped that referenced this pull request Apr 20, 2018
* 'master' of github.com:DefinitelyTyped/DefinitelyTyped: (181 commits)
  [Joi] - add schema option to .when (DefinitelyTyped#25136)
  @types/mapbox-gl maxDuration property in FlyToOptions (DefinitelyTyped#25088)
  Glue - Fix to plugins (DefinitelyTyped#25141)
  add addEventListener to eventsource (DefinitelyTyped#25125)
  Adding more siblings to Red interface in node-red (DefinitelyTyped#25013)
  [knex] Add QueryBuilder to knex.raw bindings (DefinitelyTyped#25100)
  react: change default type value of snapshot to any (DefinitelyTyped#24987)
  added typings for mapbox-gl-leaflet (DefinitelyTyped#25120)
  React animate on scroll (DefinitelyTyped#25131)
  Fixed AccountResponse's signers' object (DefinitelyTyped#25095)
  React-navigation onTransition event pass props (DefinitelyTyped#25121)
  dotenv-safe 5.0 (DefinitelyTyped#25124)
  change member types from variable to method (DefinitelyTyped#25097)
  Updating react-native-drawer definitions from 2.3 to 2.5 (DefinitelyTyped#25118)
  Added the statement of ActiveSelection (DefinitelyTyped#25084)
  [THREE] add Curve.arcLengthDivisions (DefinitelyTyped#25116)
  [atom] Support version v1.26. (DefinitelyTyped#25111)
  [adone] refactoring, additions (DefinitelyTyped#25114)
  (react-native-modalbox) Remove ViewProperties from main props (DefinitelyTyped#25128)
  added typings for npm package ng-tags-input (DefinitelyTyped#24946)
  ...
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). Unmerged The author did not merge the PR when it was ready.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

react: unable to use getSnapshotBeforeUpdate

3 participants