Skip to content

Commit 7d54883

Browse files
authored
allow function component with children (#3676)
* allow function component with children * test for 3611 * better element type * remove namespace
1 parent 1cb4b38 commit 7d54883

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export type ComponentProps<
8787
: never;
8888

8989
export interface FunctionComponent<P = {}> {
90-
(props: RenderableProps<P>, context?: any): VNode<any> | null;
90+
(props: RenderableProps<P>, context?: any): ComponentChild;
9191
displayName?: string;
9292
defaultProps?: Partial<P>;
9393
}

src/jsx.d.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import {
55
ClassAttributes,
66
Component,
7+
ComponentChild,
8+
ComponentType,
9+
FunctionComponent,
710
PreactDOMAttributes,
811
VNode
912
} from './index';
@@ -26,9 +29,14 @@ export namespace JSXInternal {
2629
key?: any;
2730
}
2831

29-
export interface Element extends VNode<any> {}
30-
31-
export interface ElementClass extends Component<any, any> {}
32+
export type ElementType<P = any> =
33+
| {
34+
[K in keyof IntrinsicElements]: P extends IntrinsicElements[K]
35+
? K
36+
: never;
37+
}[keyof IntrinsicElements]
38+
| ComponentType<P>;
39+
export type ElementClass = Component<any, any> | FunctionComponent<any>;
3240

3341
export interface ElementAttributesProperty {
3442
props: any;

test/ts/preact.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ const UseOfComponentWithChildren = () => {
102102
);
103103
};
104104

105+
const DummyChildren: FunctionalComponent = ({ children }) => {
106+
return children;
107+
};
108+
109+
function ReturnChildren(props: { children: preact.ComponentChildren }) {
110+
return props.children;
111+
}
112+
113+
function TestUndefinedChildren() {
114+
return (
115+
<ReturnChildren>
116+
<ReturnChildren>Hello</ReturnChildren>
117+
</ReturnChildren>
118+
);
119+
}
120+
105121
// using ref and or jsx
106122
class ComponentUsingRef extends Component<any, any> {
107123
private array: string[];

0 commit comments

Comments
 (0)