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

Fix the types of Component's render function #1085

Merged
merged 1 commit into from May 3, 2018

Conversation

valotas
Copy link
Contributor

@valotas valotas commented Apr 30, 2018

No description provided.

src/preact.d.ts Outdated
}

function h<P>(
node: ComponentFactory<P>,
params: Attributes & P | null,
...children: (ComponentChild | ComponentChildren)[]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ComponentChildren = ComponentChild[]

Copy link

Choose a reason for hiding this comment

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

Actually preact also accepts an array of ComponentChildren as a valid argument to h()
example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But If I understand correctly your example your are passing a ComponentChild[] (which is an alias for ComponentChildren), not a ComponentChildren[] (that would mean ComponentChild[][]).

Copy link

Choose a reason for hiding this comment

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

I'm indeed passing a ComponentChildren[] because children is a rest parameter.

Take this example and feed it the typescript compiler:

type SomeArrayType = string[];
function test(...test: SomeArrayType) {}
test(['a', 'b', 'c']);

e.g. here in the typescript playground.

And you'll get the error/warning: Argument of type 'string[]' is not assignable to parameter of type 'string'.

If you change the function signature to (...test: SomeArrayType[]) the error/warning will disapear.

Copy link
Member

Choose a reason for hiding this comment

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

Huh, it appears Preact does support ComponentChild[][] and flattens it to a single list of children: example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ACK! Removed the corresponding commit

@coveralls
Copy link

coveralls commented Apr 30, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling ba738b7 on valotas:componentChildren into 9affea6 on developit:master.

@@ -98,18 +98,18 @@ declare namespace preact {

forceUpdate(callback?: () => void): void;

abstract render(props?: RenderableProps<P>, state?: Readonly<S>, context?: any): JSX.Element | null;
abstract render(props?: RenderableProps<P>, state?: Readonly<S>, context?: any): ComponentChild;
Copy link
Member

Choose a reason for hiding this comment

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

Thanks! I just ran into this myself in my own project

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.

Awesome! Thanks for the PR 🎉

@marvinhagemeister marvinhagemeister merged commit 433fade into preactjs:master May 3, 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

5 participants