Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

fix(typings): allow to have primitives as children for built-in #369

Closed
wants to merge 1 commit into from
Closed

fix(typings): allow to have primitives as children for built-in #369

wants to merge 1 commit into from

Conversation

denis-sokolov
Copy link

What: Incorrect typing for built-in components.

Why: Because today in client code I have to add a manual typing override: <Div>{foo as any}</Div> if foo can be null.

How: By using the correct type from React library.

Checklist:

  • Documentation N/A
  • Tests
  • Ready to be merged
  • Added myself to contributors table

Fixup 08cfb31.

It is valid to use null or false in react children,
but the previous typings did not handle that case.

ReactChild is a subset of ReactNode:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/98f1d85679fc581283cf6cc4d48ce8ae8b18e4a5/types/react/index.d.ts#L170

The commit is without tests for SVG, same as the commit being fixed.

Copy link
Collaborator

@luke-john luke-john left a comment

Choose a reason for hiding this comment

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

Looks good to me, thank you for this contribution to the typings.

I'd like to look at the react node typing to see if this may result in other unexpected behaviour with css properties. I should get a chance to do this tomorrow morning. (~12hours).

@Ailrun has done some great work recently with these typings and may know.

@@ -218,14 +218,6 @@ glamorous('div', {
withProps: ''
})

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm assuming this was removed by accident as it looks like it should fail and appears unrelated to the changes. Would you mind re adding if that's the case thanks.

Copy link
Author

Choose a reason for hiding this comment

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

This is removed on purpose, as this relaxation is the side-effect of the proposed change.

The new indexed type allows the value to be boolean, which makes the primaryColor: props.visible be valid.

Of course, it’s silly that we have this effect, but as noted in the other comment, it’d be awesome to not even have children in the CSSProperties. Until then this is a workaround.

The PR is effectively relaxing the type-checking for this instance, meaning fewer errors will be caught by TypeScript compiler. However, requirements that are too lax are much better than requirements that are too strict. In the latter case the developers fall into habit into silencing errors (with as any), defeating the entire purpose of typechecking in the first place.

@@ -1926,7 +1926,7 @@ export interface CSSPropertiesLossy {
| undefined
| Array<CSSPropertiesComplete[keyof CSSPropertiesComplete]>
| CSSPropertiesLossy
| React.ReactChild
| React.ReactNode
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the update, I'd love to remove this entirely from this file as a ReactNode/ReactChild is not a css property.

But keeping things working in the meantime sounds great 💯.

Copy link
Author

Choose a reason for hiding this comment

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

Me too, but small steps at a time. :-(

I have tried investigating the root cause, but I can not understand why does children match the index signature on CSSProperties rather than the existing children on React components. I tried overriding children even directly in CSSPropertiesLossy, but the index still takes priority. By the way, if I remove CSSPropertiesLossy entirely, my code typechecks. But it, of course, might not be the solution.

Fixup 08cfb31.

It is valid to use `null` or `false` in react children,
but the previous typings did not handle that case.

`ReactChild` is a subset of `ReactNode`:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/98f1d85679fc581283cf6cc4d48ce8ae8b18e4a5/types/react/index.d.ts#L170

The commit is without tests for SVG, same as the commit being fixed.
@luke-john
Copy link
Collaborator

Hey @denis-sokolov, sorry for the delay in looking at this. Really appreciate your work on this pr and following up on the issue.

I pulled this down for a play, and ended up creating a pr which solves the issue in a different way (removing the need for React.ReactNode in CSSPropertiesLossy).

https://github.com/paypal/glamorous/pull/372/files#diff-b6bac500e3d114b9016569ad1059fdd7
#372

@denis-sokolov
Copy link
Author

If #372 works, clearly it is better than this hack. Let’s close this then, unless #372 does not work out, and then we’ll reopen this.

@denis-sokolov
Copy link
Author

Seeing how #372 is delayed, perhaps we could merge this in the meantime? (The build error is trivially fixable)

@Ailrun
Copy link
Contributor

Ailrun commented Feb 15, 2018

@denis-sokolov I will merge #372 as soon as @luke-john updates snapshot. Sorry for delaying.

@Ailrun
Copy link
Contributor

Ailrun commented Feb 16, 2018

#372 is merged. IMO, we could close this.

@Ailrun Ailrun closed this Feb 16, 2018
@denis-sokolov denis-sokolov deleted the fix-primitive-children branch February 19, 2018 08:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants