Skip to content

[types-2.0] react - better strict mode compatibility - #11935

Merged
vvakame merged 10 commits into
DefinitelyTyped:types-2.0from
beckend:linken-react-strictnull
Nov 8, 2016
Merged

vvakame merged 10 commits into
DefinitelyTyped:types-2.0from
beckend:linken-react-strictnull

Conversation

@beckend

@beckend beckend commented Oct 12, 2016 •

Copy link
Copy Markdown
Contributor

index.d.ts

  • line 16, Key was changed to accept null, When you do React.createElement(...) and you do not supply a key, it becomes the value null.
  • line 40, ComponentClass<P>, when testing React.cloneElement which is able to accept null and undefined, had to add any type.
  • line 77, DOMFactory<P, T>, using any React.DOM.xxx function to create an element. For example React.DOM.div(null, undefined) actually works and thus added.
  • line 96, a React child can be null.
  • line 202, the last commit added the return type of null to SFC<P> and in strict mode there was no longer the possibility to render the component any more, it will say null is not a constructor function for jsx elements.. A workaround is to convert it to an interface then overload the constructor function to have let SFC<P> to have the ability to return null, but leave StatelessComponent<P> alone. You now have 2 versions to work with, in strict mode SFC<P> will fail to render every time because of null return, but StatelessComponent<P> will work.
  • line 265, ComponentSpec.render, the render may return null like every other render.

The rest of the files had to be adjusted accordingly when using strictNullChecks.

@beckend beckend changed the title [types-2.0] react - strict mode compatible [types-2.0] react - better strict mode compatibility Oct 12, 2016
@beckend

beckend commented Oct 12, 2016

Copy link
Copy Markdown
Contributor Author

I don't think that this covers all null or undefined clauses but it's a start.

@donaldpipowitch

Copy link
Copy Markdown
Contributor

SFC was an alias to StatelessComponent. I'd like to keep this behaviour. I would not want to decide which interface I should use depending on my current tsconfig. For now I'd just want to revert my change: #11931.

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.

@beckend

beckend commented Oct 13, 2016

Copy link
Copy Markdown
Contributor Author

Let me know how you want to proceed.

@donaldpipowitch

Copy link
Copy Markdown
Contributor

You could include my PR #11931 and I'll close mine. The bug is reported now and I'll add | null back to the definitions when it works with TSX.

@beckend

beckend commented Oct 13, 2016

Copy link
Copy Markdown
Contributor Author

It is done.

@donaldpipowitch donaldpipowitch mentioned this pull request Oct 13, 2016
@donaldpipowitch

Copy link
Copy Markdown
Contributor

Cool. I've closed my PR.

// cc @aozgaa You merged my previous PR. Could you review this PR? Thanks.

@donaldpipowitch

Copy link
Copy Markdown
Contributor

@vvakame Do you know why this PR fails here?:

----------------- For file:/home/travis/build/DefinitelyTyped/DefinitelyTyped/ejs/tsconfig.json
\home\travis\build\DefinitelyTyped\DefinitelyTyped\ejs\index.d.ts
     Line 11: Cannot redeclare exported variable 'cache'.
\home\travis\build\DefinitelyTyped\DefinitelyTyped\ejs\index.d.ts
     Line 65: Cannot redeclare exported variable 'cache'.

I doesn't look like this PR touches ejs?

@vvakame

vvakame commented Oct 25, 2016

Copy link
Copy Markdown
Member

umm... Could you try to merge types-2.0/HEAD to your branch?

@beckend

beckend commented Oct 25, 2016

Copy link
Copy Markdown
Contributor Author

@vvakame good call, that fixed it.

@vvakame

vvakame commented Oct 28, 2016 •

Copy link
Copy Markdown
Member

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?
👍 or 👎?

Checklist

  • pass the Travis CI test?

@vvakame

vvakame commented Oct 28, 2016

Copy link
Copy Markdown
Member

When we can't get response from author. please ping me after 3 days. I'll merge this PR.

Comment thread react/index.d.ts Outdated

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>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

children should remain ReactNode[] and ReactNode should be fixed to allow undefined and null.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, that will be fixed.

Comment thread react/index.d.ts Outdated
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>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

@beckend beckend Oct 30, 2016 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@vsiao vsiao Oct 30, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea this is kind of sensitive, but that's what code reviews are for, I'm kind of unsure how to best fix this.

screenshot from 2016-10-30 15-57-08

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@beckend beckend Oct 30, 2016 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the test would become

React.cloneElement(element, {});
React.cloneElement(element, {}, null);

And pass.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vsiao can you review this so the react pulls can proceed?

@vsiao vsiao Nov 7, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread react/react-tests.ts Outdated
};

var container: Element;
var container: Element = document.createElement('div');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double quotes please

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be fixed.

Comment thread react/react-tests.ts Outdated
var StatelessComponent2: React.SFC<SCProps> =
// props is contextually typed
props => React.DOM.div(null, props.foo);
(props: SCProps) => React.DOM.div(null, props.foo);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per the comment above this line, props is supposed to be contextually typed. So please leave out the SCProps annotation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be fixed.

Comment thread react/react-tests.ts Outdated
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() : {};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be fixed.

Comment thread react/react-tests.ts Outdated
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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: Error | null?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type null is not assignable to Error .

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the ValidationMap interface needs to be fixed, perhaps?

Comment thread react/index.d.ts Outdated
type ReactType = string | ComponentClass<any> | StatelessComponent<any>;

type Key = string | number;
type Key = string | number | null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Key value 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's actually better, see it fixed for next commit.

@vsiao vsiao left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above comments

@beckend

beckend commented Oct 30, 2016

Copy link
Copy Markdown
Contributor Author

@vsiao current reviews committed.

@yuit

yuit commented Nov 7, 2016

Copy link
Copy Markdown
Contributor

@vsiao is the PR look good to you?

@vsiao

vsiao commented Nov 7, 2016 •

Copy link
Copy Markdown
Contributor

@yuit No, not yet. Thanks for asking.

@beckend

beckend commented Nov 7, 2016

Copy link
Copy Markdown
Contributor Author

@vsiao updated.

@vsiao vsiao left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@beckend: almost there! just a few nits inline.

Comment thread react/index.d.ts Outdated
type: string | ComponentClass<P> | SFC<P>;
props: P;
key?: Key;
key?: Key | null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for key to be undefined? If not, let's remove ?.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pardon me, no I have only seen a value or null, never undefined

Comment thread react/react-tests.ts Outdated

var ClassicComponent: React.ClassicComponentClass<Props> =
React.createClass<Props, State>({
displayName: 'ClassicComponent',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double quotes please

Comment thread react/react-tests.ts Outdated
var StatelessComponent2: React.SFC<SCProps> =
// props is contextually typed
props => React.DOM.div(null, props.foo);
(props) => React.DOM.div(null, props.foo);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated change here

Comment thread react/react-tests.ts Outdated
},
// 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 => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error | null

@beckend

beckend commented Nov 7, 2016

Copy link
Copy Markdown
Contributor Author

@vsiao bump.

@donaldpipowitch

Copy link
Copy Markdown
Contributor

@beckend Thank you for your fast responses.

@beckend

beckend commented Nov 7, 2016

Copy link
Copy Markdown
Contributor Author

I am waiting for this PR as well :)

@johnnyreilly

Copy link
Copy Markdown
Member

Is everyone happy for this to be merged now?

@vsiao

vsiao commented Nov 7, 2016

Copy link
Copy Markdown
Contributor

lgtm! Thanks for your work, @beckend.

@vvakame
vvakame merged commit 7f9ddef into DefinitelyTyped:types-2.0 Nov 8, 2016
@beckend
beckend deleted the linken-react-strictnull branch November 8, 2016 14:54
@donaldpipowitch

Copy link
Copy Markdown
Contributor

Hooray!

@huan086 huan086 mentioned this pull request Dec 31, 2018
9 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants