Skip to content
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

Add boolean to ComponentChild in preact.d.ts and remove object from type #1219

Merged
merged 2 commits into from Oct 1, 2018

Conversation

KasparEtter
Copy link

Fixes #1215.

@KasparEtter
Copy link
Author

Please note that I couldn't test the change as the tests don't run for me on master. Happy to iterate based on your feedback, @marvinhagemeister. 🙂

@KasparEtter
Copy link
Author

Thanks to Travis, I see that removing object from ComponentChildren breaks https://github.com/developit/preact/blob/b5c4a36576b65623c2664d04c03a04e57dc1e06a/test/ts/VNode-test.tsx#L65

The other problem is the same as on master:

The command "if [ "$TRAVIS_NODE_VERSION" = "8" ]; then COVERAGE=false FLAKEY=false PERFORMANCE=false ALLOW_SAUCELABS=true npm run test:karma; ./node_modules/coveralls/bin/coveralls.js < ./coverage/lcov.info; fi" exited with 1.

Is there a use case for passing a function (or another object) as a child to a component? Since it's likely a mistake by the programmer, I would prefer to adapt the test instead of the type declaration. On the other hand, one could argue that it's up to the component what it does with its children (including whether to evaluate them with a function call). What do you think, shall I add object again (to ComponentChild instead of ComponentChildren this time)?

Copy link
Member

@marvinhagemeister marvinhagemeister left a comment

Choose a reason for hiding this comment

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

That's awesome, thanks for making a PR 🎉 The tests are failing because type object is needed to get JSX children to work properly with TS.

A simple test case for boolean would be the icing on the cake. Something like this would be amazing to have it in VNode-test.tsx:

class BooleanComponent extends Component {
	render() {
		return false;
	}
}

The tests can be run with npm run test:ts. If it still doesn't work I'm happy to help debug further 👍

src/preact.d.ts Show resolved Hide resolved
@marvinhagemeister
Copy link
Member

marvinhagemeister commented Sep 24, 2018

@KasparEtter Passing a function as a child is a valid pattern and the way the new Context-API in React 16 works https://reactjs.org/docs/context.html#consumer (we don't support it yet in preact).

@coveralls
Copy link

coveralls commented Sep 25, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling 884cf7d on KasparEtter:component-children into b7038d1 on developit:master.

@KasparEtter
Copy link
Author

I updated my PR (by force-pushing an amended commit, I hope you don't mind that I squashed already but it was a tiny commit anyway). I hope you agree that the types belong to ComponentChild and not ComponentChildren. I also added test cases for the various types (I just realized that maybe I should also add one for VNode<any>). What I don't like about these tests is that they don't verify that the children are actually checked. However, the only thing I found after a quick search to add "negative" test cases is https://stackoverflow.com/questions/51194259/asserting-that-typescript-should-fail-to-type-check-some-example-code, which would add new dev dependencies and I don't know whether this is acceptable. (Adding at least tslint seems reasonable to me as the test file had an unused import and missing semicolons.) npm run test:ts works for me, I'm only having problems with npm run test.

Two more observation based on React's types:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/fbf888379b391b2a8519415cb1e66e26e0d0e87b/types/react/index.d.ts#L152

Copy link
Member

@marvinhagemeister marvinhagemeister left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for making the PR even better 🎉 👍 👍

@marvinhagemeister marvinhagemeister merged commit 8b2f56a into preactjs:master Oct 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants