-
Notifications
You must be signed in to change notification settings - Fork 30.5k
[react] allow weaker prop declarations for nullish properties #31280
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] allow weaker prop declarations for nullish properties #31280
Conversation
|
|
||
| export interface ReactElementLike { | ||
| type: string | ((...args: any[]) => ReactElementLike); | ||
| type: ReactComponentLike; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original type property for this was not exactly the same as the one in the React type definitions, causing issues with the PropTypes.element validator, which this fixes.
| type ExtractedPropsFromOuterPropsWithoutAnnotation = PropTypes.InferProps<typeof outerPropTypesWithoutAnnotation>['props']; | ||
|
|
||
| // $ExpectType: true | ||
| // $ExpectType true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed a longstanding issue with these DTSLint assertions
|
|
||
| type ValidationMap<T> = PropTypes.ValidationMap<T>; | ||
|
|
||
| type WeakValidationMap<T> = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The weak validation map will transform any prop declaration that can be null or undefined and widen it to be both: null | undefined. This allows props authors to declare something like this:
type Props = { foo?: string }and have it pass checks for HOCs that require it to be:
type Props = { foo?: string | null }Most developers do not realize that propTypes allows for both so this should meet a compromise of DX and runtime accuracy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ferdaber, after upgrading @types/react to 16.7.14 from 16.7.13, my project is getting
TS2605: JSX element type 'ReactElement<any> | null' is not a constructor function for JSX elements.
Type 'ReactElement<any>' is not assignable to type 'Element'.
for almost every component. I've tracked down this bug into these changes. I'm wondering if anyone else is getting this error...I'll try to create a reproducible repo soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the problem was a different issue (cc @cuyl) - I've written the fix here:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx
|
@ferdaber Thank you for submitting this PR! 🔔 @DovydasNavickas @johnnyreilly @bbenezech @pzavolinsky @digiguru @ericanderson @tkrotoff @onigoetz @theruther4d @guilhermehubner @jrakotoharisoa @pascaloliv @Hotell @franklixuefei @Kovensky - please review this PR in the next few days. Be sure to explicitly select If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead. |
|
Since you're a listed owner and the build passed, this PR is fast-tracked. A maintainer will merge shortly. If it shouldn't be merged yet, please leave a comment saying so and we'll wait. Thank you for your contribution to DefinitelyTyped! |
|
Thanks! |
|
Would this satisfy the commented out tests here? https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/test/managedAttributes.tsx |
|
Unfortunately no, it requires bumping |
|
@milesj if you were asking if those tests passed, I did test them out and they did not error for TS 3.0. |
Please fill in this template.
npm test.)npm run lint package-name(ortscif notslint.jsonis present).If changing an existing definition:
tslint.jsoncontaining{ "extends": "dtslint/dt.json" }.cc @andrewbranch