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
Conversation
src/preact.d.ts
Outdated
} | ||
|
||
function h<P>( | ||
node: ComponentFactory<P>, | ||
params: Attributes & P | null, | ||
...children: (ComponentChild | ComponentChildren)[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ComponentChildren = ComponentChild[]
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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[][]
).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
@@ -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; |
There was a problem hiding this comment.
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
There was a problem hiding this 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 🎉
No description provided.