Repository navigation
[types-2.0] react - better strict mode compatibility - #11935
Conversation
|
I don't think that this covers all |
|
I think this could be a problem with TypeScript itself, because it only happens with TSX and not with TS. I reported this here: microsoft/TypeScript#11566. |
|
Let me know how you want to proceed. |
|
You could include my PR #11931 and I'll close mine. The bug is reported now and I'll add |
|
It is done. |
|
Cool. I've closed my PR. // cc @aozgaa You merged my previous PR. Could you review this PR? Thanks. |
|
@vvakame Do you know why this PR fails here?: I doesn't look like this PR touches |
|
umm... Could you try to merge types-2.0/HEAD to your branch? |
|
@vvakame good call, that fixed it. |
|
react/index.d.ts to authors (@pspeter3 @vsiao @johnnyreilly AssureSign (account can't be detected) Microsoft (account can't be detected)). Could you review this PR? Checklist
|
|
When we can't get response from author. please ping me after 3 days. I'll merge this PR. |
|
|
||
| interface DOMFactory<P extends DOMAttributes<T>, T extends Element> { | ||
| (props?: P & ClassAttributes<T>, ...children: ReactNode[]): DOMElement<P, T>; | ||
| (props?: P & ClassAttributes<T> | null, ...children: Array<ReactNode | undefined | null>): DOMElement<P, T>; |
There was a problem hiding this comment.
children should remain ReactNode[] and ReactNode should be fixed to allow undefined and null.
There was a problem hiding this comment.
Good catch, that will be fixed.
| type CElement<P, T extends Component<P, ComponentState>> = ComponentElement<P, T>; | ||
| interface ComponentElement<P, T extends Component<P, ComponentState>> extends ReactElement<P> { | ||
| type: ComponentClass<P>; | ||
| type: ComponentClass<P> | ComponentClass<any>; |
There was a problem hiding this comment.
This is when you use for example React.cloneElement .
The elements can have ComponentClass<undefined> or ComponentClass<null> which will error.
Should you revert this change you can't clone elements in strictNullChecks or other methods that accepts ComponentClass with either null or undefined.
This is of course can be simplified by only use any but I wanted to leave some traces of what is was aiming to be.
There was a problem hiding this comment.
Can you give a concrete code example with error message(s)? Other top-level API methods rely on this being strongly typed (eg. to infer the correct return type of ReactDOM.render) so I'm worried that the any will break that.
There was a problem hiding this comment.
Because cloneElement is a coma separated argument type, props can be nothing, in case the user only wants to clone the element and supply children as the last parameter.
Otherwise we can revert this change and force the user not use null or undefined , and the user will have to supply an empty object instead, then both would be satisfied.
What do you think?
There was a problem hiding this comment.
So the test would become
React.cloneElement(element, {});
React.cloneElement(element, {}, null);And pass.
There was a problem hiding this comment.
@vsiao can you review this so the react pulls can proceed?
There was a problem hiding this comment.
Is it possible to use a null literal type? ie.
function cloneElement<P>(
element: ReactElement<P>,
props: null,
...children: ReactNode[]): ReactElement<P>;If not, I would bias towards reverting this change and force users to pass an empty object rather than messing with the type of ComponentElement.
| }; | ||
|
|
||
| var container: Element; | ||
| var container: Element = document.createElement('div'); |
| var StatelessComponent2: React.SFC<SCProps> = | ||
| // props is contextually typed | ||
| props => React.DOM.div(null, props.foo); | ||
| (props: SCProps) => React.DOM.div(null, props.foo); |
There was a problem hiding this comment.
per the comment above this line, props is supposed to be contextually typed. So please leave out the SCProps annotation.
| var defaultProps: Props = ClassicComponent.getDefaultProps(); | ||
| var propTypes: React.ValidationMap<Props> = ClassicComponent.propTypes; | ||
| var displayName: string | undefined = ClassicComponent.displayName; | ||
| var defaultProps: Props | {} = ClassicComponent.getDefaultProps ? ClassicComponent.getDefaultProps() : {}; |
There was a problem hiding this comment.
can you keep : Props = and cast the empty object (<Props>{}) instead? Otherwise, if getDefaultProps() has the wrong return type it'll get assigned to {} and won't break.
| requiredFunc: React.PropTypes.func.isRequired, | ||
| requiredAny: React.PropTypes.any.isRequired, | ||
| customProp: function(props: any, propName: string, componentName: string) { | ||
| customProp: function(props: any, propName: string, componentName: string): any { |
There was a problem hiding this comment.
Type null is not assignable to Error .
There was a problem hiding this comment.
Then the ValidationMap interface needs to be fixed, perhaps?
| type ReactType = string | ComponentClass<any> | StatelessComponent<any>; | ||
|
|
||
| type Key = string | number; | ||
| type Key = string | number | null; |
There was a problem hiding this comment.
You can use React.createElement or other methods that created elements, the return type should reflect the actual possible types a variable can actually become.
So if you do something with the returned payload it warns you that the value can actually be null . I don't actually get the question here since the Key value can actually be null from the built in methods, regardless of necessity, this reflects the actual React API workings.
There was a problem hiding this comment.
the
Keyvalue can actually be null from the built in methods
Great, that's what I'm asking about. Did you consider changing the key property on interface ReactElement<P> to key: Key | null instead?
There was a problem hiding this comment.
That's actually better, see it fixed for next commit.
|
@vsiao current reviews committed. |
|
@vsiao is the PR look good to you? |
|
@yuit No, not yet. Thanks for asking. |
|
@vsiao updated. |
| type: string | ComponentClass<P> | SFC<P>; | ||
| props: P; | ||
| key?: Key; | ||
| key?: Key | null; |
There was a problem hiding this comment.
Is it possible for key to be undefined? If not, let's remove ?.
There was a problem hiding this comment.
Pardon me, no I have only seen a value or null, never undefined
|
|
||
| var ClassicComponent: React.ClassicComponentClass<Props> = | ||
| React.createClass<Props, State>({ | ||
| displayName: 'ClassicComponent', |
| var StatelessComponent2: React.SFC<SCProps> = | ||
| // props is contextually typed | ||
| props => React.DOM.div(null, props.foo); | ||
| (props) => React.DOM.div(null, props.foo); |
| }, | ||
| // https://facebook.github.io/react/warnings/dont-call-proptypes.html#fixing-the-false-positive-in-third-party-proptypes | ||
| percentage: (object: any, key: string, componentName: string, ...rest: any[]): Error => { | ||
| percentage: (object: any, key: string, componentName: string, ...rest: any[]): any => { |
|
@vsiao bump. |
|
@beckend Thank you for your fast responses. |
|
I am waiting for this PR as well :) |
|
Is everyone happy for this to be merged now? |
|
lgtm! Thanks for your work, @beckend. |
|
Hooray! |

index.d.ts
Keywas changed to accept null, When you doReact.createElement(...)and you do not supply a key, it becomes the valuenull.ComponentClass<P>, when testingReact.cloneElementwhich is able to acceptnullandundefined, had to addanytype.DOMFactory<P, T>, using anyReact.DOM.xxxfunction to create an element. For exampleReact.DOM.div(null, undefined)actually works and thus added.null.nulltoSFC<P>and in strict mode there was no longer the possibility to render the component any more, it will saynull is not a constructor function for jsx elements.. A workaround is to convert it to an interface then overload the constructor function to have letSFC<P>to have the ability to return null, but leaveStatelessComponent<P>alone. You now have 2 versions to work with, in strict modeSFC<P>will fail to render every time because ofnullreturn, butStatelessComponent<P>will work.ComponentSpec.render, the render may returnnulllike every other render.The rest of the files had to be adjusted accordingly when using
strictNullChecks.